Adding New Items to Your ASP.NET 5 Project with Yeoman Subgenerators

Hi Sayed here, this post is graciously authored by Shayne Boyer who is a member of the ASP.NET community that I’ve been working with lately. Enjoy.

Sourabh Shirhatti did a great write up on the use of yeoman and the ASP.NET generator for creating projects, http://blogs.msdn.com/b/webdev/archive/2014/12/17/yeoman-generators-for-asp-net-vnext.aspx , but another advantage of yeoman is subgenerators for adding additional items to our project.

Creating a new Project

To summarize, creating a quick “HelloWorldMvc” application is easy with the aspnet generator.

$ yo aspnet

You will get the response of what type of project you want to create. Notice that there is a new “Class Library” project type now as well.

     _-----_
    |       |    .--------------------------.
    |--(o)--|    |      Welcome to the      |
   `---------´   |   marvellous ASP.NET 5   |
    ( _´U`_ )    |        generator!        |
    /___A___\    '--------------------------'
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y ` 

? What type of application do you want to create? (Use arrow keys)
❯ Console Application 
  Web Application 
  MVC Application 
  Nancy ASP.NET Application 
  Class Library

Select the MVC Application, and the generator asks for the name of the application. Enter “HelloWorldMvc”; press enter and the scaffolding will commence and the project template is created.

spboyer-mac-dev:HelloWorldMvc shayneboyer$ tree
.
├── Controllers
│   └── HomeController.cs
├── Models
│   └── User.cs
├── Startup.cs
├── Views
│   ├── Home
│   │   └── Index.cshtml
│   └── Shared
│       └── _Layout.cshtml
├── project.json
└── wwwroot

Adding New Items to Your Project

If you have been Visual Studio user, you are familiar with the Add > New Item dialog

Screen Shot 2014-12-06 at 10.21.39 AM

when needing to add Controllers, Views, Class, etc. type files to your project. However, when developing in other IDEs such as Sublime Text or Brackets obviously this is not an option.

yo aspnet –help

Using this command, you can see all of the subgenerators that are available.

$ yo aspnet --help

Usage:
  yo aspnet:app [options] 

Options:
  -h,   --help  # Print generator's options and usage  Default: false

  --skip-install # Skip triggering kpm restore automatically  Default: false

Description:
    Creates a basic ASP.NET 5 application

Subgenerators: 

    yo aspnet:BowerJson [options] 
    yo aspnet:Class [options] <name>
    yo aspnet:CoffeeScript [options] <name>
    yo aspnet:Gruntfile [options] <name>
    yo aspnet:HTMLPage [options] <name>
    yo aspnet:JScript [options] <name>
    yo aspnet:JSON [options] <name>
    yo aspnet:MvcController [options] <name>
    yo aspnet:MvcView [options] <name>
    yo aspnet:PackageJson [options] 
    yo aspnet:StartupClass [options] 
    yo aspnet:TextFile [options] <name>
     yo aspnet:WebApiController [options] <name>

Adding any of the items is simple, move to the directory you want to have the new item placed in and execute the generator.

$ cd Controllers
$ yo aspnet:MvcController ContactController
You called the aspnet subgenerator with the arg ContactController
ContactController.cs created.
   create ContactController.cs

Now looking at the overall project tree I have added the new Controller and View.

├── Controllers
│   ├── ContactController.cs
│   └── HomeController.cs
├── Models
│   └── User.cs
├── Startup.cs
├── Views
│   ├── Contact
│   │   └── Index.cshtml
│   ├── Home
│   │   └── Index.cshtml
│   └── Shared
│       └── _Layout.cshtml
├── project.json
└── wwwroot

Yo Whats next?

Get involved, project is hosted on GitHub at omnisharp/generator-aspnet. I enjoyed adding the subgenerators and see opportunities for growth already. What would you like to see?

Shayne Boyer

@spboyer on twitter

http://www.tattoocoder.com