Routing In MVC 5

In this article, I will explain about routing in MVC.

Introduction

 
Here I will explain how many types of routing there are in MVC and how it works in the project.
 

What is Routing?

 
Routing is a pattern matching system. Routing maps an incoming request (from the browser) to particular resources (controller & action method). This means routing provides the functionality to define a URL pattern that will handle the request. That is how the application matches a URI to an action. 
 
URL Pattern 
  • domain.com/Home/about/1
    domain.com is a namespace or hosting name
    Home is a Controller name
    about is an action method name
    1 is id 
General process of request: Client sends the request to the server and processes it and produces the output.
 
Routing In MVC 5
Routing In MVC 5
 

How To Work Routing

 
Routing parses the request in route configuration. Then, it matches the request in the route table to ensure which controller and which action will be processed.
 
Routing In MVC 5
 

Route

 
Basically, Route is an approach which defines URL pattern and handles the information. In MVC, routes decide which Controller method to be executed on a particular request.
 
An MVC URL consists of the following properties.
  • Route Name: A route is a URL pattern that is mapped to a handler.
  • A handler has a controller that can process the request.
Configure a Route
 
Each MVC application has a default route which is defined in RouteConfig class under the App_Start folder. The following image describes the configuration of a Route in the RouteConfig class.
Routing In MVC 5
In the above image, you see the Maproute(). it is a method of RouteCollection class used to configure a route in our application. It contains the name "Default". The URL pattern is "{controller}/{action}/{id}" and default parameter is for controller, action method, and id parameter.
 

Route Table

  • We define a route for each action method.
  • All the routes are stored in the route table.
  • Each incoming request is mapped to this route table.
  • If a URL match is found then request goes to the related controller action method.
  • If the URL is not found, the application returns a 404 page.

Types of Routing

 
There are 2 types of Routing in MVC application
  • Conventional or Traditional Routing (Using Routing Config)
  • Attribute Routing (Available in MVC 5) 

Conventional or Traditional Routing

  • Conventional or Traditional Routing also is a pattern matching system for URL that maps incoming request to the particular controller and action method.
  • We set all the routes in the RouteConfig file.
  • RouteConfig file is available in the App_Start folder.
  • We need to register all the routes to make them operational.

Route

 
Route is a pattern for a particular URL. It needs the following parameters.
  • Name
  • Pattern
  • Default values
  • Constraints (if any) 
Example
  1. routes.MapRoute(  
  2.                 name: "Default",  
  3.                 url: "{controller}/{action}/{id}",  
  4.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  5.             );  
RouteConfig file
  1. public class RouteConfig  
  2.     {  
  3.         public static void RegisterRoutes(RouteCollection routes)  
  4.         {  
  5.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  6.   
  7.             routes.MapRoute(  
  8.                 name: "Default",  
  9.                 url: "{controller}/{action}/{id}",  
  10.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  11.             );  
  12.         }  
  13.     }  
 Register RouteConfig in Global.asax
  1. protected void Application_Start()  
  2. {  
  3.       AreaRegistration.RegisterAllAreas();   
  4.       RouteConfig.RegisterRoutes(RouteTable.Routes);   
  5. }  

Attribute Routing

 
It is a very simple routing method compared to conventional routing. All the concepts are just like the conventional approach but here, we define all the routes and attributes. In attribute, we define the routing on a simple controller or action method.  
 
How to register attribute routing
 
For simply registering the attribute routing in our application, we need to add the following line in routeconfig.cs.
  1. routes.MapMvcAttributeRoutes();  
Routing In MVC 5
 
How to use attribute routing
 
Simply, we can use Routeprefix or route attribute for using attribute routing in the controller.
  1.    [RoutePrefix("Home")]  
  2.    public class HomeController : Controller  
  3.    {  
  4.        [Route("Index")]  
  5.        public ActionResult Index()  
  6.        {  
  7.            return View();  
  8.        }  
  9.    }  

Summary

 
In this article, I have discussed MVC routing. I hope you will find it useful.