Setup MVC in Asp.Net Core application using AddControllersWithViews method
The default structure of all dot net core applications is the same. To enable the MVC (Model View Controller), we need to add AddControllersWithViews
service in the ConfigureServices
method.
In dot net core we have following common templates for web applications
- Web API
- Razor pages
- MVC (Model View Controller)
When we create the application by using the template then the methods are added automatically by the system.
Let’s learn how to use the AddControllersWithViews
method in an empty application.
Open the startup class and navigate to the ConfigureServices
method. Add the following code to it.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
Adding AddControllersWithViews
will add –
- Services required for Controller
- Services required for Views
- And everything else required for MVC application.
Click here to learn the difference between
AddMVC
andAddControllersWithViews
.
Add controller in asp.net core
After adding the AddControllersWithViews services, Let’s add a new folder to store the controller files.
- Create a new folder with the name
Controllers
at the root level. - Add a new class
HomeController
inside this Controllers folder. - Inherit this Home Controller from Microsoft.AspNetCore.Mvc.
Controller
class. - Your class will look like below –
using Microsoft.AspNetCore.Mvc;
namespace LearnAspNetCore.Controllers
{
public class HomeController : Controller
{
// Code goes here
}
}
- Let us add a very simple method in this HomeController class.
using Microsoft.AspNetCore.Mvc;
namespace LearnAspNetCore.Controllers
{
public class HomeController : Controller
{
public string Index()
{
return "WebGentle";
}
}
}
To get the data from HomeController, we need to enable the routing.
Enable default routing
Let’s open the Startup
class again and navigate to the Configure
method.
Let’s make some changes to the UseEndpoints
method.
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
Now Run the application by pressing f5
(if using VS) or dotnet run
command.
You must see the “WebGentle” in the browser.
At this point, you might think –
- How did the application know about the home controller and Index action method?
- What if I add a controller with some other name (instead of HomeController)?
- What if I add the action method with some other name (Instead of Index)?
Will the application still display the message “WebGentle”. Well go ahead and try to make the changes. (You will learn more once you will experiment by yourself.)
I believe you have tried all the above options and you still don’t see any message on the browser.
Let’s go to the definition of the MapDefaultControllerRoute
method.
// Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder
// and adds the default route {controller=Home}/{action=Index}/{id?}.
Focus on the second line adds the default route {controller=Home}/{action=Index}/{id?}
.
Here you can see the default controller is Home
and the default action method is Index
.
It means if you are not passing anything else in the URL then h default will be /home/index
else you need to pass the name of your new controller and action method manually in the URL like /employee/list