Saturday, October 24, 2015

Creating a Sample ASP.NET 5 App to Run in DNX

In my on-going quest to better understand the .NET Execution Environment (DNX) and all of its goodness, I've been creating sample applications. Recently I've been experimenting with the Yeoman generators for ASP.NET. In this post, I want to introduce Yeoman and run through the steps necessary to quickly create a simple MVC application that can be run with DNX.

Yeoman is billed as "The web's scaffolding tool for modern webapps." It is built on top of NodeJS and uses generators to create whatever project or file is requested. A developer calls Yeoman, specifying a generator, to kickstart new projects, encapsulating all of the necessary details for a given technology stack.

Microsoft has had a similar scaffolding concept built into the File > New Project menu item in Visual Studio. Most .NET developers expect this type of templating to be available to them. Since DNX is enabling cross-platform development in the .NET stack, and Visual Studio is not cross platform, Microsoft is leaning on Yeoman to provide the scaffolding for ASP.NET 5.

Installing Yeoman is very easy once NodeJS, especially the Node Package Manager (npm), is installed. (NodeJS can be downloaded from the NodeJS web site.) Open a command-prompt and execute the following command:

npm install -g yo
This command installs Yeoman globally for everyone. To call Yeoman, execute the command
yo
from a command prompt. It will display a text menu of options, including generating applications with any installed generators.

Once Yeoman is installed, the generators for ASP.NET 5 need to be installed. They are installed by executing the command

npm install -g generator-aspnet
This command installs the ASP.NET 5 generators globally.

The following steps can be taken to use Yeoman to create a basic MVC 6 application that will run in the DNX. These instructions assume you already have the DNX installed.

  1. Call Yeoman:
    yo aspnet
    1. Select Web Application Basic from the menu presented and hit Enter.
    2. Name your application HelloWorldMvc at the next prompt.
  2. Change directory to your new MVC application folder:
    cd HelloWorldMvc
  3. Restore NuGet packages:
    dnu restore
  4. Run the application:
    dnx kestrel
  5. Navigate to the application in your browser by going to http://localhost:5000
  6. You should see the default page with the name HelloWorldMvc in the upper left-hand corner.

Congratulations! You have created and executed an ASP.NET 5 application in the .NET Execution Environment.

No comments:

Post a Comment