Custom Routing in ASP.NET MVC

Custom Routing in ASP.NET MVC Application

In this article, I am going to discuss Custom Routing in ASP.NET MVC Application. Please read our previous article before proceeding to this article where we discussed the basics of Routing in ASP.NET MVC Application. 

As we already discussed the Routing is a pattern matching mechanism which handles the incoming request (i.e. incoming URL) and figures out what to do with that incoming request (i.e. incoming URL). The following diagram shows the routing process work in ASP.NET MVC Application.

Custom Routing in ASP.NET MVC Application

In our previous article, we discussed the default route that is created by MVC Framework and how it handles the incoming request. But if you want to follow your own convention then you would need to modify the default route or you need to create your own route. Let’s discuss how to create your own route in ASP.NET MVC application.

Creating Custom Routes in ASP.NET MVC Application:

You can configure your custom route using MapRoute extension method. You need to provide at least two parameters in MapRoute function i.e. route name and URL pattern. The Defaults parameter is optional.

You can register multiple custom routes with different names. Consider the following example where we register “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 = "Process", 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:

As shown in the above code, 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 default controller and action to handle any URL request which starts from domain name/Employee.

MVC framework evaluates each route in sequence. It starts with the first configured route and if incoming URL doesn’t satisfy with 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 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 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 routing system checks the incoming URL pattern form 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 Constraint in ASP.NET MVC Routing. Here, in this article, I try to explain the Custom Routing in ASP.NET MVC Application step by step with a simple example. I hope this article will help you with your need. 

1 thought on “Custom Routing 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. Required fields are marked *