Part of a Series

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.

project.json

JavaScript

1

2

3

4

5

6

7

8

9

10

11

{

...

"dependencies":{

"Microsoft.AspNet.Server.IIS":"1.0.0-beta4",

"Microsoft.AspNet.Server.WebListener":"1.0.0-beta4",

"Microsoft.AspNet.Mvc":"6.0.0-beta4"

},

...

}

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.

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.

1

2

3

4

publicvoidConfigureServices(IServiceCollection services)

{

services.AddMvc();

}

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

1

2

3

4

5

6

7

8

9

publicvoidConfigure(IApplicationBuilder app)

{

app.UseMvc(routes=>

{

routes.MapRoute(

name:"default",

template:"{controller=Home}/{action=Index}/{id?}");

});

}

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

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.

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.

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

1

2

3

4

5

6

7

8

9

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

Hello,world!

</body>

</html>

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:

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.