Over a million developers have joined DZone.

Creating an ASP.NET 5 Empty Application

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

This blog post is part of a series covering ASP.NET 5  To follow along, this post requires installing Visual Studio 2015 (currently in preview).

To accelerate the learning process for ASP.NET 5 (aka ASP.NET vNext), this post will cover:

  • Creating the project
  • Examining the project
  • Modifying the project
  • Running the project

The goal of these topics is not only to show “how-to”, but to answer the “what is that…” and “why is that” as well.  It is written as if you are following along in Visual Studio 2015 (tutorial style), yet with enough supplemental detail to simply read (article style). Although not an exhaustive dive into each area, it should provide enough context to quickly get started with ASP.NET 5.

Creating the project

To create a new ASP.NET 5 application, it is necessary to think in terms of projects, not web sites.  The Web Site… option as seen below is still supported in Visual Studio 2015 to support legacy site development.  The specific “ASP.NET vNext” features require the Project… option. With this in mind, in Visual Studio 2015 select from the top menu bar: File --> New --> Project...

image

After selecting Project…, a new project dialogue box appears.  On the left side of the box, select the Web option under the language of preference (Visual C# used below).  From there, select ASP.NET Web Application and choose the solution/project name and file location.  This post will use ASPNET5 as the solution name, and ASPNET5Blank as the project name.

image
ASP.NET 5 Class Library and Console Application project templates will be covered in an upcoming blog post

Another dialogue box will appear to fine tune the type of web project desired.  Choose the ASP.NET 5 Empty template as seen below.  Although you may see other options, proceed forward with default values (items remain unchecked).

image

Examining the project

Once the project has loaded in Visual Studio, Solution Explorer will reveal the following:

image

The project corresponds to the following file/folder structure:

image

The following is a brief explanation of the projects file/folder contents:

Project_Readme.html HTML help file (not required to be in project, can be discarded)
*.kproj File for managing ASP.NET 5 projects in Visual Studio
*.kproj.user File for managing project user options in Visual Studio
wwwroot Folder of static files served to client. No code or ‘razor’ goes here
project.json File for managing project settings
Startup.cs File for startup and configuration code

The first three items above are there for the benefit of the developer (help file) and Visual Studio (*.kproj.* files).  The latter three are needed for development.

Let’s take a look at the initial contents of the project.json file:

{
    "webroot": "wwwroot",
    "version": "1.0.0-*",
    "exclude": [
        "wwwroot"
    ],
    "packExclude": [
        "**.kproj",
        "**.user",
        "**.vspscc"
    ],
    "dependencies": {
        "Microsoft.AspNet.Server.IIS": "1.0.0-beta1"
    },
    "frameworks" : {
        "aspnet50" : { },
        "aspnetcore50" : { }
    }
}

As can be seen above, the file contains name/value pairs (see schema at GitHub).  This new approach to managing web configurations (notice no web.config) enables simplified dependency management.  Instead of adding assembly references, you now add NuGet packages as dependencies.  How is this better?  Instead of hard-coupling a reference to an assembly, you can now simply add the dependency by just typing it into the file above.

The contents of the Startup.cs file are as follows:

using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;

namespace ASPNET5Blank
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            // In empty template, no code is here...
        }
    }
}

The Configure method of the Startup class is automatically called at runtime. The IApplicationBuilder instance (with parameter name app highlighted above) provides methods to configure the app and to access the HTTP request.  An example of the UseWelcomePage method is used in the article Create a Web API in MVC 6.  However, when I attempt to access this method in my project, I do not see it:

image

The only method displayed is Use.  In the next topic, the project will be modified to enable the use of other methods off of the IApplicationBuilder instance passed in. 

Modifying the Project

In Visual Studio, open the project.json file. We are going to add a dependency to the Microsoft.AspNet.Diagnostics package which is needed to get access to the UseWelcomePage method. Under dependencies, the name of the package is provided paired with the version.  It must be typed accurately.  However, when doing so in Visual Studio, we get discovery and auto-completion via IntelliSense!

image

In the above screen capture, I started adding the Microsoft.AspNet.Diagnostics package.  When ready to add the corresponding value for version, IntelliSense came to the rescue again!

image

After saving the project.json file, the output window in Visual Studio reveals what happened:

image

The Microsoft.AspNet.Diagnostics package was installed along with its dependencies.  Did this modify the project.json file to show the two new packages?  No.  And that is good news.  Why?  There is no need to clutter the file with hierarchies of dependencies.  When a new package is referenced, the dependencies (and their dependencies) are automatically added.  When removed, all dependencies are removed too!

Returning to the Startup.cs file, when I now attempt to access the UseWelcomePage method, it exists!

image

The Configure method is now updated to look as follows:

public void Configure(IApplicationBuilder app)
{
    app.UseWelcomePage();
}

What will this do?  Let’s find out!

Running the Project

By default, the project will be deployed to IIS Express for testing.  The classic Debug menu options still work here:

visual studio debugging options

When selecting either Start Debugging [F5] or Start Without Debugging [Ctrl+F5], the app will run showing the welcome screen as seen here:

usewelcomepage screen

Summary

This post examined the very essentials of an ASP.NET 5 application. In addition to learning how to create, modify, and run an app, we poked around with the initial files and learned how cool dependencies are managed in the project.json file. 

Be sure to check the series covering ASP.NET 5 for new and updated topics!

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Michael Palermo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}