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.
- What is Routing in ASP.NET Core MVC?
- What are the different types of Routing supported by ASP.NET Core MVC?
- How Routing is working in ASP.NET Core?
- 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:
- Convention Based Routing
- 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.
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.
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.
Let us have a look at the implementation of the UseMvcWithDefaultRoute() method by visiting the following GitHub URL.
The implementation is given as shown below.
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.
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.