Routing in ASP.NET MVC

Routing is a method for processing incoming URLs that provides the intended result and is more informative.

Conventional routing: this sort of routing is defined by calling the MapRoute method, by setting its unique name, URL pattern, and a few default settings.

  • URL routing will consider the domain name. The URL pattern "{controller}/{action}/{id}" resembles localhost:1234/{controller}/{action}/{id}. 
  • The default controller and action method will be used to handle requests when the URL does not contain any more information following the domain name.
  • When ASP.NET MVC starts, it registers one or more patterns with the framework route table to tell the routing engine what to do with requests matching those patterns. When a request is received by the routing engine during runtime, it checks the URL against its URL patterns and returns the response.

 

Attribute-based routing is defined by specifying the Route attribute in the controller's action method.

 

Routing maps URL patterns to handlers and handles browser requests for MVC controllers. Routing helps define URL structure and map URLs to controllers. Three routing segments are crucial.

  1. ControllerName
  2. ActionMethodName
  3. Parammeter

 

Attribute-based routing A route attribute is added to an action method.

/URL: /saveData
[Route("saveData")]
public ActionResult Index()
   ViewBag.Message = "Welcome to ASP.NET MVC!";
   return View();
}

Optional Parameter Attribute Routing  
The URL pattern can also define an optional parameter by marking the route parameter ("?"). We can declare the default value with parameter=value.   

// Optional URI Parameter
// URL: /saveData/
// URL: /saveData/Deepak

[Route("saveData/{ customerName ?}")]
public ActionResult OtherTest(string customerName){
     ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}

// Optional URI Parameter with default value
// URL: /saveData/
// URL: /saveData/Deepak

[Route("Mvctest /{ customerName =Talwar}")]
public ActionResult OtherTest(string customerName) {
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}


Since ASP.NET MVC 5 was introduced, two distinct kinds of routing have been available. Convention-based routing & Attribute-based routing


Conventional/ Default routing: 

The default ASP.NET MVC project templates include a generic route that breaks a request URL into three named parts using the following URL convention. : "

{controller}/{action}/{id}" 

The MapRoute() extension function of RouteCollection registers this route pattern.

 

public static void RegisterGlobalFilters(GlobalFilterCollection filters){
      filters.Add(new HandleErrorAttribute());
}
protected void Application_Start() {
   AreaRegistration.RegisterAllAreas();
   RegisterGlobalFilters(GlobalFilters.Filters);
   RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes) {
       routes.MapRoute(
         "Default", // Route name
          "{controller}/{action}/{id}", // Route Pattern
          new {
             controller = "Home",
             action = "Index",
             id = UrlParameter.Optional
         }// Default values for above defined parameters
    );
}

 

A 404 HTTP status code indicates that the routing engine cannot handle the request because the URL does not match any route patterns.

Author
Full Stack Developer

Deepak Talwar

Technical Architect & Full Stack Developer with 18+ years of Professional Experience in Microsoft Technologies & PHP Platform. Hands on experience with C#, VB, ASP.NET, ASP.NET MVC, ASP.NET Core, ASP.NET Web API, Linq, ADO.NET, Entity Framework, Entity Framework Core, Sql Server, MYSQL, NoSql, Javascript, Angular, jQuery, AWS, Azure, React, Angular, Laravel, Codeingiter, Serenity, VBA, Cloud Computing, Microservices, Design Patterns, Software Architecture, Web Design and Development.

Related Post