Creating Custom Routes in ASP.NET MVC

How to Create Custom Routes in ASP.NET MVC Application

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.

  1. What is Custom Routing in MVC?
  2. Why do we need Custom Routing in ASP.NET MVC?
  3. 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 article, we already discussed this.

Custom Routing in ASP.NET MVC Application

In our previous article, we discussed the default route that is created by ASP.NET MVC Framework and how it handles the incoming request. But if you don’t 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. Let’s 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.

namespace FirstMVCDemo
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Employee",
                url: " Employee/{id}",
                defaults: new { controller = "Employee", action = "Index" }
            );
            
            routes.MapRoute(
                name: "Default", //Route Name
                url: "{controller}/{action}/{id}", //Route Pattern
                defaults: new
                {
                    controller = "Home", //Controller Name
                    action = "Index", //Action method Name
                    id = UrlParameter.Optional //Defaut value for above defined parameter
                }
            );
        }
    }
}

So, in this way you can configure as many as routes you want with your own URL pattern in ASP.NET MVC Application. Let’s add Employee Controller to our application

namespace FirstMVCDemo.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}
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 haven’t 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 doesn’t 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 doesn’t 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.

  1. http://localhost:53605/Employee
  2. http://localhost:53605/Employee/Index
  3. 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.

In the next article, I am going to discuss Route constraints in ASP.NET MVC Routing. Here, in this article, I try to explain the How to Create Custom Routes in ASP.NET MVC Applications with Examples. I hope this How to Create Custom Routes in the MVC article will help you with your needs. 

4 thoughts on “Creating Custom Routes in ASP.NET MVC”

  1. Hi, for this custom route below, should the default controller be referring to “Employee” instead of “Process”?

    routes.MapRoute(
    name: “Employee”,
    url: ” Employee/{id}”,
    defaults: new { controller = “Process”, action = “Index” }
    );

Leave a Reply

Your email address will not be published.