Types of Routing in MVC

There are 2 types of Routing in MVC application

1. Conventional or Traditional Routing (Using Routing Config)

2. Attribute Routing (Available in MVC 5) 


1. 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.

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

Register RouteConfig in Global.asax
protected void Application_Start()  
{  
      AreaRegistration.RegisterAllAreas();   
      RouteConfig.RegisterRoutes(RouteTable.Routes);   
}  



2. 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.  
 
For simply registering the attribute routing in our application, we need to add the following line in routeconfig.cs.
routes.MapMvcAttributeRoutes();  


Enabling Attribute Routing
To enable Attribute Routing, we need to call the MapMvcAttributeRoutes method of the route collection class during configuration.
public class RouteConfig  
{  
    public static void RegisterRoutes(RouteCollection routes)  
    {  
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
        routes.MapMvcAttributeRoutes();  
    } 
}

Attribute Routing Example
A route attribute is defined on top of an action method. The following is the example of a Route Attribute in which routing is defined where the action method is defined.
In the following example, I am defining the route attribute on top of the action method

public class HomeController : Controller  
{  
    //URL: /Mvctest  
    [Route(“Mvctest”)]  
    public ActionResult Index()  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
}}

Attribute Routing with Optional Parameter
We can also define an optional parameter in the URL pattern by defining a question mark (“?") to the route parameter. We can also define the default value by using parameter=value.

public class HomeController : Controller  
{  
  
  // Optional URI Parameter  
  // URL: /Mvctest/  
  // URL: /Mvctest/0023654  
  
    [Route(“Mvctest /{ customerName ?}”)]  
    public ActionResult OtherTest(string customerName)  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }  
  
   // Optional URI Parameter with default value  
  // URL: /Mvctest/  
  // URL: /Mvctest/0023654  
  
   [Route(“Mvctest /{ customerName =0036952}”)]  
   public ActionResult OtherTest(string customerName)  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }  


Route Prefixes
We can also set a common prefix for the entire controller (all action methods within the controller) using the “RoutePrefix” attribute.

[RoutePrefix(“Mvctest”)]  
public class HomeController : Controller  
{  
    // URL: /Mvctest/  
    [Route]  
    public ActionResult Index()  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }  
  
  // Optional URI Parameter  
  // URL: /Mvctest/  
  // URL: /Mvctest/0023654  
    [Route(“{ customerName }”)]  
    public ActionResult OtherTest(string customerName)  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }  

Defining Default Route using Route Attribute
We can also define a Route attribute on top of the controller, to capture the default action method as the parameter.

[RoutePrefix(“Mvctest”)]  
[Route(“action=index”)]  
public class HomeController : Controller  
{  
    // URL: /Mvctest/  
    public ActionResult Index()  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }   
  
    // URL: /Mvctest/NewMethod  
    public ActionResult NewMethod()  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }  

Defining Route name
We can also define a name of the route to allow easy URI generation.

[Route(“Mvctest”,  Name = "myTestURL")]  
public ActionResult Index()  
{  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
We can generate URI using Url.RouteUrl method.

<a href="@Url.RouteUrl("mainmenu")">Test URI</a>  
Defining Area

We can define the "Area" name from the controller that belongs to the using RouteArea attribute. If we define the “RouteArea” attribute on top of the controller, we can remove the AreaRegistration class from global.asax.
[RouteArea(“Test”)]  
[RoutePrefix(“Mvctest”)]  
[Route(“action=index”)]  
  
public class HomeController : Controller  
{  
    // URL: /Test/Mvctest/  
    public ActionResult Index()  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        return View();  
    }