Routing In ASP.NET MVC

Routing in ASP.NET MVC directs a request to Controller. It matches the URL with the configured route pattern. Routine Engine uses Route table for matching the pattern for URL request. URL pattern can be registered in Route table in RouteConfig class in App_Start folder and System.Web.Routing namespace is used by MVC framework.

Routing

Routing directs a request to Controller. It matches the URL with the configured route pattern. Routine Engine uses Route table for matching the pattern for URL request. URL pattern can be registered in Route table in RouteConfig class in App_Start folder and System.Web.Routing namespace is used by MVC framework.

You can register your route in RegisteRoutes() method of RouteConfig file.

The sample of default registered route is given below. 

  1. public class RouteConfig {  
  2.     public static void RegisterRoutes(RouteCollection routes) {  
  3.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.         routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  5.             controller = "Home", action = "Index", id = UrlParameter.Optional  
  6.         });  
  7.     }  
  8. }   

Now,

Line 3- IgnoreRoute restricts routing of the provided route.

Line5- MapRoute registers the route having 3 parameters.

Line 7- Name of the route

Line 8- URL pattern of the route

Line 9- Defaults of the route.

You can add multiple routes with different names using MapRote method.

The sample is given below for the same. 

  1. public static void RegisterRoutes(RouteCollection routes) {  
  2.     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  3.     routes.MapRoute(name: "Employee", url: "employee/{id}", defaults: new {  
  4.         controller = "Employee", action = "Index"  
  5.     });  
  6.     routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  7.         controller = "Home", action = "Index", id = UrlParameter.Optional  
  8.     });  
  9. }  
  10. }   

In the example given above, we have registered two routes, where first has name Employee and another one is Default.

Constraint

If you want to restrict the request with the specific criteria, you can use the constraints.

In the example given below, we are restricting id parameter for only numeric parameter. 

  1. routes.MapRoute(name: "Student", url: "student/{id}/{name}/{standardId}", defaults: new {  
  2.     controller = "Student", action = "Index", id = UrlParameter.Optional,  
  3.         name = UrlParameter.Optional, standardId = UrlParameter.Optional  
  4. }, constraints: new {  
  5.     id = @ "\d+"  
  6. });   

In line 6, we have added the constraint.

Now, all the routes are registered in Application_Start() in Global.asax.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace FirstAspNetMVC {  
  8.     public class FistMvcApplication: System.Web.HttpApplication {  
  9.         protected void Application_Start() {  
  10.             AreaRegistration.RegisterAllAreas();  
  11.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  12.         }  
  13.     }