Back to: ASP.NET Core Tutorials For Beginners and Professionals
How to Set up MVC in ASP.NET Core Application
In this article, I am going to discuss How to Set up MVC in ASP.NET Core Application step by step. Please read our previous where we give you a brief Introduction to ASP.NET Core MVC Framework.
It is possible in ASP.NET Core (.NET) to build an entire application using only the ASP.NET Core Middleware. But the ASP.NET Core MVC framework provides the features we can use to create HTML Pages and HTTP-Based APIs easily. So, here I will show you how to set up MVC in ASP.NET Core Application. To do so, let us first create an ASP.NET Core Empty Application. Later on, in this ASP.NET Core MVC article series, we will see examples using the ASP.NET Core Web Application (Model-View-Controller) Project template.
Creating a new ASP.NET Core Empty Application:
To create a new Empty ASP.NET Core Web Application, open Visual Studio 2022 and click on the Create a new project tab, as shown in the image below.
Once you click on the Create a new project tab, it will open the Create a new project window. From this window, you need to select the ASP.NET Core Empty project template and click the Next button, as shown in the image below.
Once you click on the Next button, it will open the Configure Your New Project window. Here, you need to provide the necessary information to create a new project. First, give an appropriate name for your project (FirstCoreMVCWebApplication), set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. And finally, click on the Create button, as shown in the image below.
Once you click on the Next button, it will open the Additional Information window. Here, you need to select .NET 6.0 as the Framework. You also need to check the Configure for HTTPS and Do not use top-level statements check boxes, and finally, click on the Create button as shown in the below image.
That’s it. Once you click on the Create Button, the project is going to be created with the Empty template with the following folder and file structure.
The Empty template, by default, does not include the setup for MVC. Now let us see how to set up MVC in the ASP.NET Core application.
Setup MVC in ASP.NET Core Application:
Two simple steps are required to set up MVC in ASP.NET Core Application.
Adding MVC Service to the Request Processing Pipeline:
First, we need to add the required MVC services to the Application Request Processing Pipeline. To do so, we need to modify the Main method of the Program class, which is present within the Program.cs class file as shown below. The following piece of code will include all the required services which are required to develop the ASP.NET core MVC application. Once you add this, you can use Models, Controllers, Views, and many other features in your ASP.NET Core MVC Application.
Note: Along with AddMVC() method, we also have AddControllersWithViews() method. In the next article, we will discuss these two methods in detail as well as we will also discuss the difference between these two methods and when to use one over another.
Adding Controller in ASP.NET Core MVC Application:
In the ASP.NET Core MVC application, all the Controllers should be present within a specific folder called Controllers. So first, create a folder with the name Controllers within the project root directory. Once you add the Controllers folder, add a new class file named HomeController.cs within the Controllers folder. Once you add the HomeController class, your project folder structure should be as shown below.
Now open the HomeController.cs class file and copy and paste the following code. In order to make a class as a controller in ASP.NET Core MVC, that class needs to be inherited from the Controller base class. So, you can see in the below code our controller, i.e., HomeController, is inherited from the Controller base class. This Controller base class belongs to Microsoft.AspNetCore.Mvc namespace.
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCWebApplication.Controllers { public class HomeController : Controller { public string Index() { return "This is Index action from MVC Controller"; } } }
With the above changes in place, now run the application, and you will get the following output.
We are not getting the output from the Index action method of the HomeController. The above output is coming from the below code, which you can find within the Main method of the Program class.
How to Configure our Index Action Method of HomeController as the Default Route?
We need to tell the ASP.NET Core MVC Framework to use the Index action method of our Home Controller as the default route. To do so, we need to add the Required MVC Middleware Component to the application request processing pipeline. To do so, we need to use the UseRouting and UseEndPoints Middleware components, as shown in the below image.
UseRouting(): It matches a request to an endpoint.
UseEndpoints(): It executes the matched endpoint.
So, modify the Main Method of the Program class as shown below. The MapDefaultControllerRoute() method adds the MVC Middleware to the Request Processing Pipeline of the ASP.NET Core Application.
namespace FirstCoreMVCWebApplication { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add MVC services to the container. builder.Services.AddMvc(); var app = builder.Build(); app.UseRouting(); app.UseEndpoints(endpoints => { //Configuring the MVC middleware to the request processing pipeline endpoints.MapDefaultControllerRoute(); }); //app.MapGet("/", () => "Hello World!"); app.Run(); } } }
Now, run the application, and you should get the output as expected, as shown in the below image.
Understanding MapDefaultControllerRoute() Middleware Component in ASP.NET Core
Let’s have a look at the definition of the MapDefaultControllerRoute() middleware. You will see the following if you go to the MapDefaultControllerRoute() method’s definition.
The above image shows that the default controller is Home, and our application’s default action method is Index. This is the reason why when we run the application, the Index action method of the Home Controller handles the request. But if you want, then you can also change this default behavior, which we will discuss in our upcoming articles.
So, in short, to set up MVC in the ASP.NET Core Web Application, first, we need to add the required MVC Services to the dependency injection container, and secondly, we need to configure the MVC Middleware components in the Request Processing Pipeline.
In the next article, I am going to discuss the differences between AddController(), AddMvc(), AddControllersWithViews(), and AddRazorPages() Methods and when to use one over another in ASP.NET Core MVC application. In this article, I try to explain How to set up MVC in ASP.NET Core Web Application step by step with an example. I hope you enjoy this MVC Setup in ASP.NET Core Web Application article.
why this tutorial not working for ASP.net core 3.1. Great Jobe!
Initially, this tutorial was created based on .NET Core 2.2. But now we are updating it to .NET 6. In a few days, you will find everything related .NET 6.
Is the this tutorial available in .Net core 3.1
No. This Course Targeted Framework is .NET 6