Nov
13
2015

Taking Command of ASP.NET 5

I had a great time recording a video on Channel 9 that walked through getting started with ASP.NET 5 just using a command line and text editor of choice (I used notepad and Visual Studio Code).  This post is based on that video, breaking it down into steps you can follow.  Although the content shown is based on Windows 10 using a command prompt, you can follow along with Linux or Mac as long as you (likely) know your command line equivalent.  I only use notepad in this post, but if you installed Visual Studio Code (optional below), just replace all occurrences of notepad in the command line with code instead.

Please make sure you have the following installed:

At the command prompt create a ‘solution’ level folder.  I will name mine demo.

C:\md demo

Navigate into the demo folder.

C:\cd demo

Confirm the latest runtime is installed.

C:\demo\dnvm upgrade

You should have a visual confirmation of the latest version. It will indicate runtimes added in your user profile path, though if they were previously installed, nothing was duplicated. View your list of installed runtimes.

C:\demo\dnvm list

Depending on your environment, you will see one or more runtimes installed. An asterisk preceding a line indicated the default runtime. Create a file named nuget.config.

C:\demo\break>nuget.config

Open the new file in text editor of choice. In this example, I will open with notepad.

C:\demo\notepad nuget.config

In text editor, type the following markup in the nuget.config file and save it:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

The content of the above nuget.config provides context for where nuget packages can be found for any projects (subfolders) in this solution (current directory). Make sure you have saved the file. Back at the command line, create a subdirectory of demo named src.

C:\demo\md src

Navigate into the src folder.

C:\demo\cd src

Create a file named project.json

C:\demo\src\break>project.json

Open project.json in a text editor

C:\demo\src\notepad project.json

In text editor, type the following JSON notation in the project.json file and save it:

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta8"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451" :  { },
    "dnxcore50" :  { }
  },

  "exclude": [
    "wwwroot"
  ]
}

The content of the above project.json provides project-level information such as where static file content will be located (wwwroot folder) and thus excluded from any compilation. It also indicates what dependencies are required, as well as versioning and supported commands (web).  Make sure you have saved the file.  Back at the command line, run the .NET utility (dnu) to restore packages indicated in the project.json file.

C:\demo\src\dnu restore

After running the above command, you should see information indicating packages are being restored and from what nuget source (it should be the one you defined in the nuget.config file). Back at the command line, create a new file named Startup.cs.

C:\demo\src\break>Startup.cs

Open Startup.cs in text editor.

C:\demo\src\notepad Startup.cs

In text editor, type the following code in the Startup.cs file and save it:

using Microsoft.AspNet.Builder;

namespace CommandDemo
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
		app.UseWelcomePage();
        }
    }
}

The above code file will display a simple welcome page (generated via Microsoft.AspNet.Diagnostics, a dependency in project.json) when web site is accessed. Make sure you saved the file.  Back at the command line, use the .NET execution tool (dnx) to start the web server via the web command (defined in project.json).

C:\demo\src\dnx web

After running the above command, you will receive information about the web server starting, how to access the server (likely http://localhost:5000), and how to stop the server. Open a web browser and navigate to the URL specified in the console window. You should see something similar to this:

SNAGHTML481f6ac

If you see something like the above content, congrats!  You just made an ASP.NET 5 web site completely from scratch using nothing but the command line and a simple text editor.  If you did not see the content above, be sure to check for any spelling/syntax errors in all of the files.  Leave a comment if you have any questions or feedback.

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).

Resources

Archives