In this article, I am going to discuss How to Create Custom Routes in the ASP.NET MVC Applications with Examples. Please read our previous article before proceeding to this article where we discussed the basics of Routing in the ASP.NET MVC Application. At the end of this article, you will understand the following pointers in detail which are related to ASP.NET MVC Custom Routing.
- What is Custom Routing in MVC?
- Why do we need Custom Routing in ASP.NET MVC?
- How to Create Custom Route in ASP.NET MVC?
As we already discussed the ASP.NET MVC Routing is a pattern matching mechanism that handles the incoming HTTP request (i.e. incoming URL) and figures out what to do with that incoming HTTP request. The following diagram shows how the routing work in ASP.NET MVC Framework and in our previous Video, we already discussed this.
In our previous Video, we discussed the default route that is created by ASP.NET MVC Framework and how it handles the incoming request. But if you dont want to follow the default URL Pattern rather you want to create your own URL Pattern then you need to either modify the default route or you need to create your own route. Lets discuss how to create our own route in ASP.NET MVC application.
Creating Custom Routes in ASP.NET MVC Application:
As we already discussed, if we want to configure any routes then we need to configure the routes within the RegisterRoute method of RouteConfig class using the MapRoute extension method. While configuring the Routes, at least two parameters we need to provide to the MapRoute method i.e. Route name and URL pattern. The Default parameter is optional.
The point that you need to remember is, the Route Names must be unique. You can register multiple custom routes with different names. Consider the following example where we register the “Employee†route.
So, in this way you can configure as many as routes you want with your own URL pattern in ASP.NET MVC Application. Lets add Employee Controller to our application
Code Explanation:
The URL pattern for the Employee route is Employee/{id} which specifies that any URL that starts with domainName/Employee, must be handled by EmployeeController. Notice that we havent specified {action} in the URL pattern because we want every URL that starts with Employee should always use Index action of EmployeeController. We have specified the default controller and action to handle any URL request which starts from the domain name/Employee.
The ASP.NET MVC framework evaluates each route in sequence. It starts with the first configured route and if the incoming URL doesnt satisfy the First URL pattern of the route then it will evaluate the second route and so on. In the above example, the routing engine will evaluate the Employee route first and if the incoming URL doesnt start with domainName/Employee then only it will consider the second route which is the default route.
The following URLs will be mapped to the Employee route.
- http://localhost:53605/Employee
- http://localhost:53605/Employee/Index
- http://localhost:53605/Employee/Index/3
Note: Always put the more specific route on the top order while defining multiple routes, since the routing system checks the incoming URL pattern from the top and as it gets the matched route it will consider that. It will not check further routes after the matching pattern.