Loading

ASP.NET Core

How to Set up MVC in ASP.NET Core?. The Complete ASP.NET Core Developer Course 2023 [Videos].

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 article before proceeding to this article where we discussed the Basics of ASP.NET Core MVC Application.

It is possible in ASP.NET Core to build an entire application using only the asp.net core middlewares. But the ASP.NET Core MVC framework provides us the features that we can use to create HTML pages and HTTP-based APIs easily. So, here we will see how to set up MVC in ASP.NET Core Application. To do so, let us first create an Empty ASP.NET Core Web Application. Later on this ASP.NET Core MVC Video series, we will see examples using the Web Application (Model-View-Controller) template.

Creating a new ASP.NET Core Application:

To create a new Empty ASP.NET Core Web Application, First, open Visual Studio 2019 and then click on the Create a new project tab as shown in the below image.

Creating a new ASP.NET Core Application

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 Web Application template and then click on the Next button as shown in the below image.

ASP.NET Core Web Application

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, the solution name for the ASP.NET Core Web application. And finally, click on the Create button as shown in the image below.

How to Set up MVC in ASP.NET Core Application

Once you click on the create button, it will open the Create a new ASP.NET Core Web Application window where you can select the project template i.e. which type of project you want to create. As we are going to do everything manually and also from the scratch, so, select the Empty Project template and uncheck all the checkboxes from the Advanced section and finally click on the Create button as shown in the below image.

How to Set up MVC in ASP.NET Core Application

Thats 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.

Setup MVC in ASP.NET Core Application

The Empty template by default does not include the setup for MVC. Now let us see how to set up MVC in asp.net core application.

Setup MVC in ASP.NET Core Application:

There are two simple steps required to set up MVC in ASP.NET Core Application.

Adding MVC Service to the Dependency Injection Container:

First, we need to add the required MVC services to the dependency injection container. To do so you need to modify the ConfigureServices() method of the Startup class which is present within the Startup.cs class file as shown below. The following piece of code will include all the required services which are required to develop ASP.NET core MVC application. Once you add this, you can use Models, Controllers, Views, and many other features in your ASP.NET Core Application.

Adding MVC Service to the Dependency Injection Container

Note: Along with AddMVC() method, we also have AddControllersWithViews() method. In the next Video, 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 Application:

In ASP.NET Core MVC application, all the Controllers should be present within a specific folder called Controllers. So first we need to add the Controllers folder within the root project folder. Once you add the Controllers folder then add a new class file with the name HomeController within the Controllers folder. Once you add the HomeController class, your project folder structure should be as shown below.

Adding Controller in ASP.NET Core Application

Now open the HomeController class and then copy and paste the following code in it. 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 class. This Controller 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 see the output as shown in the below image.

AddControllersWithViews()

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 in the Configure method of Startup class.

How to configure our Index Action Method of HomeController?

How to configure our Index Action Method of HomeController?

We need to tell to the 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 to the application request processing pipeline. So, modify the Configure Method of Startup class as shown below. The MapDefaultControllerRoute() method add the MVC middleware to the request processing pipeline.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
//Configuring the MVC middleware to the request processing pipeline
endpoints.MapDefaultControllerRoute();
});
}

Now, run the application and you should get the output as expected as shown in the below image.

how to Setup MVC in ASP.NET Core Application step by step

Understanding MapDefaultControllerRoute() middleware

Let have a look at the definition of the MapDefaultControllerRoute() middleware.

Understanding MapDefaultControllerRoute() middleware

As shown in the above image, the default controller is Home and the default action method is Index for our application. 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 and that we will discuss in our upcoming Videos.

So, in short, to Setup MVC in asp.net core application, first we need to add the required MVC services to the dependency injection container and secondly, we need to configure the MVC middleware in the request processing pipeline.

See All

Comments (234 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /234

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /234

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /234