Tech Junkie Blog - Real World Tutorials, Happy Coding!: Hour 4: ASP.NET Core : Create Our First Controller and View

Friday, July 26, 2019

Hour 4: ASP.NET Core : Create Our First Controller and View

In the previous post we have enabled MVC on our application.  Now we want to add our first MVC controller and view to test out verify that MVC is working.  We also have to tell ASP.NET Core what pattern to look for when looking for our controllers and views.

Step-By-Step Instructions:

1.  Add a folder call "Controllers" in the root of your application

2. Right click on the "Controllers" folder and select "Add" → "New Item" → .NET Core → MVC Contoller - Empty, then click "Add"

Type HomeController when prompted

3.  You see the "HomeController.cs" class being added to the "Controllers" folder

4.  Double click on the "HomeController.cs" file and you will see the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace NorthwindCafe.Web.Controllers
    public class HomeController : Controller
        // GET: //
        public IActionResult Index()
            return View();

5.  Create a folder in the root of the application call "Views" then inside the "Views" folder create another folder call "Home".  MVC is a framework that works best when you follow the convention, by creating the folders in this hierarchy MVC knows where to look and does the plumbing for you.  If you notice the home folder matches the controller name of the HomeController.  With routing you can deviate from this convention, but it takes more work.

6.  Right click on the "Home" folder and select Add → New Item → ASP.NET Core → Razor View, keep the name of the view as Index.cshtml then click "Add"

7 .  Double click on the view "Index.cshtml" and type "Hello World" between the <div> tag like the markup below

    Layout = null;

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width" />
        Hello World!

9.  To enable routing for our application, so that MVC knows how to find our view, open up the Startup.cs file and modify the app.UseMvc() method's config instance to configure the routes for our application

        public void Configure(IApplicationBuilder app)
            app.UseMvc(config => {
                    name: "Default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new  { controller = "Home", action = "Index" }

The code above configures the default route for our application with name of the route assigned to "Default", the "template:" field specifies the pattern that MVC should look for when locating our routes.  The "defaults:" field uses an anonymous object to define a default route in case nothing is found or nothing is typed into the URL.

10. Add the line services.AddMvc(); to the ConfigureServices method like so:

        public void ConfigureServices(IServiceCollection services)

The above code uses dependency injection to inject MVC into the application, whenever it is needed. 11.  Now when you press Ctrl+5 you will see our "Hello World"

If you encounter the following error: You forgot to add the line services.AddMvc(); to the ConfigureServices method.

github Repository:

Previous: Hour 3: Enable MVC On ASP.NET Core Application

No comments:

Post a Comment

Search This Blog