Creating your first ASP.NET MVC6 Application from Empty

If you are like me, you like playing with things to a point where you understand them. When I started with ASP.NET, I followed a very nice tutorial on getting started with Bootstrap. It involved adding a NuGet package for Bootstrap and then dragging the supplied content to my view for inclusion. It was actually pretty straight forward. Doing this in ASP.NET vNext isn’t nearly as straight forward as we will see, but the resulting scaffolding will allow you to build your application outside of Visual Studio as well. I am using the Visual Studio 2015 CTP6 for this tutorial and I will have expected you to have gone through a tutorial on your first ASP.NET vNext application, of which there are many. This will ensure your environment is set up for ASP.NET development.

We are concerned with the Empty Web Project. So I started with File -> New Project… and selected “ASP.NET Web Application”. When prompted, I selected the “ASP.NET 5 Preview Empty” template. Everything else is the same. The result of this is a project with the following:


That isn’t too much – certainly not the fully functional web site you get with your Starter Web project. This is bare bones. Let’s start with creating a a basic MVC site with one controller and a page layout. Firstly, we need to create some locations for all this. To be simple, I’m going to use the default locations for Controllers and Views. I’m also going to create a folder outside called Layouts so that my layout components are separated from the rest of the views. Here is what the folder layout looks like now:


In order to produce the basics, I need to do the following:

  1. Add the ASP.NET MVC bits to my project
  2. Set up MVC in my Startup.cs
  3. Add a controller
  4. Add a view
  5. Add a page layout

We probably won’t see much until we’ve done all of these.

Add the ASP.NET MVC bits to the project

Let’s start with adding the bits. You could right-click on the poject and select “Manage NuGet Packages…”, then search for MVC, Install it and start using it. This is exactly what you would do in Visual Studio 2013 and it still works. I like editing code though and having a little more control. I also find typing is faster than clicking. I would definitely use the old method if I didn’t know what I was looking for precisely. For this task, I know what I need. Let’s use the new way.

The new way is to define your code dependencies in project.json. If you open up this file, you will note that there isn’t a lot in there. We have a section called “dependencies” that is a good place to start. Intellisense is your friend. You need to update the dependencies section to add Microsoft.AspNet.Mvc – just start typing:

"dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
    "Microsoft.AspNet.Mvc": "6.0.0-beta3"

Did you notice that Intellisense was actually helping you out with this? I particularly love that it dropped down the available versions for me. Before you save, open up the Output window at the Package Manager Log (using View->Output). Now save the file. Note that Visual Studio recognizes the change and downloads all the required packages for you. if you expand the References in your Solution Explorer, you will see the Microsoft.AspNet.Mvc has been added. If you expand that, you will see all the dependent packages as well.

Set up MVC in the Startup.cs

The empty Startup.cs file is, well, empty. We need to do three things. Firstly, we need to add the MVC service to the application. We do this in the ConfigureServices() method:

        public void ConfigureServices(IServiceCollection services)

Next, we need to tell our application to use MVC:

        public void Configure(IApplicationBuilder app)

Finally, an optional bit. We need to adjust the configuration so that we can specify a default route. If we don’t do this then we will need to go to a specific route – the home page will be an error. To do this, alter the UseMvc() call as follows:

			app.UseMvc(r => {
					name: "default",
					template: "{controller}/{action}/{id?}",
					defaults: new { controller = "Home", action = "Index" });

If you have written any ASP.NET MVC code before, the MapRoute call should be familiar ground. It’s just in a different place.

Add a Controller

Since we have defined a route to a Home controller, we should create one. Just right-click on the Controllers folder and select Add -> New Item…


You can use the search box just like you used to, or just find it. Selecting the MVC Controller Class will make the Name HomeController.cs, which is exactly what we need. Your resultant HomeController.cs should look like this:

using Microsoft.AspNet.Mvc;

// For more information on enabling MVC for empty projects, visit

namespace WebApplication1.Controllers
    public class HomeController : Controller
        // GET: /<controller>/
        public IActionResult Index()
            return View();

Sure, we could do more here. But we are trying to get a minimal project going here.

Add a View

We will do practically the same thing for the view. Let’s create a new directory under Views called Home. Right-click on the Home folder and select Add -> New Item… This time, select MVC View Page. You will note that the name changes to Index.cshtml, which happens to be what we want. There are a few things to do here. Firstly, I’m going to delete the comment at the top. Then I’m going to uncomment the ViewBag.Title. Finally, I want to add some code into the page to show some text.

    ViewBag.Title = "Home Page";


Add a Page Layout

It wouldn’t be ASP.NET if we weren’t constructing pages. We need two files. The first is ~/Layouts/PageLayout.cshtml file, which we can create in the same way as the View – just a different location. We need to model a full layout here and we need to include the all important @RenderBody() call:

<!DOCTYPE html>
	<meta charset="utf-8">

We also need to add a _ViewStart.cshtml file to the Views directory to ensure that we get the right layout. This file goes in the Views directory. The content of this file is as follows:

	Layout = "~/Layouts/PageLayout.cshtml";

Now you can run your page – click on the IIS Express button at the top. The IIS Express server will start, followed by your browser of choice. Hopefully, your <h1> contents in the Index.cshtml will be displayed. You can also use the F12 tools to ensure that your PageLayout.cshtml is wrapping it.

This is as minimal as it gets. We do need to do major work to get client-side libraries included, but everything you need for server-side processing is in this project now.