Custom Routing in MVC

Custom Routing in MVC

ASP.NET MVC Routing is a pattern matching mechanism that handles the incoming request (i.e. incoming URL) and figures out what to do with that incoming request (i.e. incoming URL). In this article, I will talk about Custom Routing in ASP.NET MVC application.

Custom Routing in ASP.NET MVC

In the last article, we discussed the basics of routing in ASP.NET MVC application. In that 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:

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.

Let’s add EmployeeController to your application

namespace FirstMVCDemo.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}
Let’s Understand the Code:

As shown in the above code, 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, 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 table shows how different URLs will be mapped to 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 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 matching pattern.

In the next article, I will discuss Route Constraint in ASP.NET MVC Routing.

If you want to learn more then please visit the MSDN site.

SUMMARY

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. I would like to have your feedback. Please post your feedback, question, or comments about this article.

1 thought on “Custom Routing in 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 *