Create Custom Routes

How to create custom Routing in mvc?

In this article, we'll explore how to define custom routes in an ASP.NET MVC application, providing you with practical examples. If you haven't already, please refer to our previous article where we covered the fundamentals of routing in ASP.NET MVC. By the end of this article, you'll gain a thorough understanding of creating and configuring custom routes within your ASP.NET MVC applications.


Defining Custom Routes in an ASP.NET MVC Application:

As previously discussed, to configure custom routes, we need to define them inside the RegisterRoutes method of the RouteConfig class using the MapRoute extension method. While setting up a route, we must specify at least two parameters: the route name and the URL pattern. The default parameters are optional. One important thing to keep in mind is that route names must be unique. You can define multiple custom routes, each with a distinct name. Let's look at an example where we define a custom route named "Employee."


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
                }
            );
        }
    }
}
                                        

This approach allows you to define multiple routes with custom URL patterns in your ASP.NET MVC application. Now, let's add an EmployeeController to our project.



namespace FirstMVCDemo.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}
                                        

Code Explaination

The URL pattern defined for the Employee route is Employee/{id} , meaning any request that begins with domainName/Employee will be directed to the EmployeeController. Since the {action} parameter is not included in the pattern, all requests matching this route will automatically invoke the Index action of the EmployeeController. Additionally, we have set default values for the controller and action to ensure proper handling of requests that start with domainName/Employee. The ASP.NET MVC framework processes routes in a sequential manner. It begins by evaluating the first configured route, and if the incoming request does not match its pattern, it moves on to the next route, continuing this process until a match is found. In the given example, the routing engine first checks if the request starts with domainName/Employee. If it doesn’t, then the next available route, which is the default route, will be considered. Below are some examples of URLs that will be mapped to the Employee route.