Notes from building a first ASP.Net Core App (part 6)

  1. I want to create an MVC application. To do this, I’m going to need to add another package and configure it. But I don’t need to install a Nuget package for this, it’s already in the environment. All we need to do is add it to project.json. Add the line of code as follows:
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0",
          "type": "platform"
        "Microsoft.AspNetCore.Diagnostics": "1.0.0",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
        "Microsoft.Extensions.Configuration.Json": "1.0.0",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0",
        "Microsoft.AspNetCore.Mvc":  "1.0.0"

    The save the project.json file, and it will restore what it needs to support MVC within the application.

  2. We need to be able to route to a controller class in MVC. The default controller in MVC is the HomeController. We need to add a HomeController. So create a Controllers folder under wwwroot, and add a HomeController.cs file. asp-net-core-file-system-2
  3. Inside that class, we will need to implement the default action, which is called Index, as follows:
    namespace MyFirstAspNetCoreApp.wwwroot.Controllers
        public class HomeController
            public string Index()
                return "MVC in ASP.Net Core Rocks!";
  4. Now we need to wire up the application to intercept the MVC request and serve up the contents of the Index action. In the Startup class, just before the app.Run, add the following line:
  5. If you mouseover that method and display the intellisense,  it says “Adds MVC to the IApplicationBuilder request execution pipeline with a default route named ‘default’ and the following template: ‘{controller=Home}/{action=Index}/{id?}’. This is why the controller needs to be called HomeController, and the method needs to be called Index. Its how the mvc default route pipeline works.
  6. Build and run the application (F5). You should see it display the text from the controller:
    MVC in ASP.Net Core Rocks!
  7. Note that it has terminated the pipeline, and doesn’t continue through to the “Hello World!…” statement.
  8. Now we want to set up the routing. Routing is performed by using the URL to redirect to a controller and action. The controller is a class and the action is a method on that class.
  9. To configure routing, change the app.UseMvcWithDefaultRoute() to this:
    app.UseMvc(routes =>
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
  10. This will be much more familiar to you if you’ve done a fair bit of MVC in the old framework. If you create a new project with ASP.Net Code and select the Web Application template, you will see that this is exactly what is does. Home is a default, Index is a default, and ? is optional.
  11. You can now add any controllers and actions you like, just by adding the controller classes, their actions, and their route mapping, using the same methods above.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: