Loading

ASP.NET Core

What is Routing in ASP.NET Core MVC?. The Complete ASP.NET Core Developer Course 2023 [Videos].

Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints. Endpoints are the app's units of executable request-handling code. Endpoints are defined in the app and configured when the app starts.

Routing in ASP.NET Core MVC Application

In this Video, I am going to discuss Routing in ASP.NET Core MVC application with examples. Please read our previous Video where we discussed with one real-time example. As part of this Video, we are going to discuss the following pointers in detail.

  1. What is Routing in ASP.NET Core MVC?
  2. What are the different types of Routing supported by ASP.NET Core MVC?
  3. How Routing is working in ASP.NET Core?
  4. Understanding Conventional based Routing.
What is Routing in ASP.NET Core MVC?

The Routing in ASP.NET Core MVC application is a mechanism in which it will inspect the incoming Requests (i.e. URLs) and then mapped that request to the controllers and their action methods. This mapping is done by the routing rules which are defined for the application. We can do this by adding the Routing Middleware to the request processing pipeline.

So, the ASP.NET Core Framework maps the incoming Requests i.e. URLs to the Controllers action methods based on the routes configured in your application. You can configure multiple routes for your application and for each route you can also set some specific configurations such as default values, constraints, message handlers, etc. If this is not clear at the moments then dont worry we will discuss each and everything with examples.

What are the different types of Routing supported by ASP.NET Core MVC?

In ASP.NET Core MVC application, you can define routes in two ways. They are as follows:

  1. Convention Based Routing
  2. Attribute-Based Routing.
What is Conventional Based Routing in ASP.NET Core MVC Application?

In Conventional Based Routing, the route is determined based on the conventions defined in the route templates which will map the incoming Requests (i.e. URLs) to controllers and their action methods. In ASP.NET Core MVC application, the Convention based Routes are defined within the Configure method of the Startup.cs class file.

What is Attribute-Based Routing in ASP.NET Core MVC Application?

In Attribute-Based Routing, the route is determined based on the attributes which are configured either at the controller level or at the action method level. We can use both Conventional Based Routing and Attribute-Based Routing in a single application.

In this Video, we are going to discuss the Conventional Based Routing and in our upcoming Video, we will discuss the Attribute-Based Routing.

Understanding Conventional Based Routing in ASP.NET Core MVC:

In ASP.NET Core MVC application, it is the controller action method that is going to handle the incoming Requests i.e. URLs. For example, if we issue a request to the â€œ/Home/Index” URL, then it is the Index action method of Home Controller class which is going to handle the request as shown in the below image.

Conventional Based Routing in ASP.NET Core MVC

Similarly, if you issue a request to the â€œ/Home/Details/2” URL, then it is the Details action method of the Home Controller class which is going to process that request as shown in the below image. Here the parameter value 2 is automatically mapped to the id parameter of the Details action method.

Conventional Routing in ASP.NET Core MVC

Now, the question that should come to your mind is, we have not explicitly defined any routing rules for the application, then how does this mapping is done i.e. how the “/Home/Index” URL is mapped to the Index action method and how “/Home/Details/2” URL is mapped to the Details action method of the Home Controller class.

This is actually done by the MVC Middleware which we registered in the applications request processing pipeline.

Understanding the Default Route in ASP.NET Core MVC Application:

As we already discussed in our previous Video that we can add the required MVC middleware into the request processing pipeline either by calling the UseMvcWithDefaultRoute() method or by calling the UseMvc() method within in the Configure() method of the Startup.cs class file as shown in the below image. As of now, we are using the UseMvcWithDefaultRoute() middleware.

Understanding the Default Route in ASP.NET Core MVC Application

Let us have a look at the implementation of the UseMvcWithDefaultRoute() method by visiting the following GitHub URL.

https://github.com/aspnet/Mvc/blob/release/2.2/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs

The implementation is given as shown below.

UseMvcWithDefaultRoute() method Implementation

As you can see in the above implementation, this method internally calls the UseMvc() method which will add the default route into the applications request processing pipeline. The default route is created with the following URL template:

{controller=Home}/{action=Index}/{id?}

Understanding The Route Template:

The above default route template maps most URLs that have the following pattern.

http://localhost:52190/Student/Details/2

The first segment path of the URL i.e. “/Student” is mapped to the “StudentController“. As you can see in the URL we do not have the word Controller with the first segment path of the URL. But it maps to the StudentController, this is because when ASP.NET Core MVC Framework finds the word /Student as the first segment path of URL, then it internally appends the word Controller.

The second segment path of the URL i.e. “/Details” is mapped to the “Details(int id)” action method of the HomeController class and the third segment path of the URL i.e. “2” is mapped to the “id” parameter of the Details(int id) action method.

As you can see in the default route template â€œ{controller=Home}/{action=Index}/{id?}“, we have a question mark at the end of the id parameter which makes the parameter id as optional. That means the following two requests now map to the same Details action method of the Home Controller class.

/Home/Details/1
/Home/Details 

In the default route template â€œ{controller=Home}/{action=Index}/{id?}“, the value â€œHome” in {controller=Home} is the default value for the Controller. Similarly the value â€œIndex” in {action=Index} is the default value for the action method. 

That means if we navigate to the applications root URL then as shown, then that request is going to be handled by the Index action method of the Home Controller class.

http://localhost:52190

The following two URLs are also mapped to the Index action method of the HomeController class.

http://localhost:52190/Home

http://localhost:52190/Home/Index

For most of the ASP.NET Core MVC applications, the default route works fine. For example, create a controller with the name as StudentController and then copy and paste the following code in it.

using Microsoft.AspNetCore.Mvc;
namespace FirstCoreMVCApplication.Controllers
{
public class StudentController : Controller
{
public string Index()
{
return "Index() Action Method of StudentController";
}
public string Details(string id)
{
return "Details() Action Method of StudentController";
}
}
}

Now, the URL â€œ/student/index” is mapped to the Index() action method of the StudentController class and the URL â€œ/student/details” is mapped to the Details() action method of the StudentController


See All

Comments (245 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 /245

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 /245

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 /245