New ASP.NET 5 Project from Scratch with Visual Studio 2015 – Part 2

No Comments

Part of a Series

This post is part 2 of a series:
1. New ASP.NET 5 Project from Scratch with Visual Studio 2015 – Part 1
2. New ASP.NET 5 Project from Scratch with Visual Studio 2015 – Part 2 (this one)

If you’re not already somewhat familiar with ASP.NET 5, I’d recommend reading part 1 first. In that post, I showed you how to create a new empty ASP.NET 5 project in Visual Studio 2015. Now I’ll show you how to set up that project for ASP.NET MVC

Why Not Web Forms?

Ok, it’s worth mentioning why I chose MVC instead of Web Forms. There may actually have been several reasons, but the main one is that Web Forms is not supported in ASP.NET 5. In parallel with ASP.NET 5, Microsoft is also releasing ASP.NET 4.6, which is the evolution of the previous versions of ASP.NET, and it does support Web Forms. In this way, ASP.NET 4.6 is the evolutionary change, and ASP.NET 5 is more for a revolutionary change. As a result, some things are different in 5 and not backwards-compatible.

Get On with It Already

The first step to get MVC in our ASP.NET project is to add the Microsoft.AspNet.MVC NuGet package as a dependency for our project. We do that in the project.json file which I introduced in my previous post.

Once you save your change to project.json, Visual Studio will automatically download the new package and place it in the References folder for all your project’s target frameworks.

2015-07-08 23_48_29-FromScratch - Microsoft Visual Studio

Next we need to tell ASP.NET to inject MVC as a dependency using its new dependency injection framework. We’ll do this during the app startup by adding a line of code to the ConfigureServices method in Startup.cs.

Now we can edit the Configure method in Startup.cs to configure MVC’s default route. Notice that the default controller and action are now specified in the route syntax

Now we can add our Controllers and Views folders, just like we used to in ASP.NET 4.x.

2015-07-09 00_02_43-FromScratch - Microsoft Visual Studio

The new HomeController can now be added by right-clicking on the Controllers folder and selecting Add > New Item… and the the MVC Controller Class template.

2015-07-09 00_04_55-Add New Item - FromScratch

The new HomeController class comes with an Index action by default, so now we can add the View to the Views folder. First we must create a Home folder inside Views to match the controller name, then Add > New Item… and selecting the MVC View Page template.

2015-07-09 00_11_10-Add New Item - FromScratch

I added a simple HTML5 template with the requisite “Hello, world” in Index.cshtml so we can tell if it works:

And now it’s time to hit F5 to see what we get. If you’ve been following along at home, you should see something like this:

2015-07-09 00_15_06-localhost_48051

Congratulations, you just built an ASP.NET 5 (MVC 6) project!

Great, What’s Next?

In the next post, we’ll look at adding client-side dependencies using Bower. In future posts we’ll cover running client-side tasks with grunt.

You can subscribe to updates above and be notified first when these future blogs are posted.