Attribute Routing and Parameters in MVC5

I assume you are familiar with MVC5 and its various features. One of the interesting features of MVC5 is attribute routing. We understand routing in MVC; we can say it is the most important feature of the Model, View and Controller design pattern. It shows the real beauty of separation of concerns. In all previous versions of MVC, we saw that the routing mechanism is defined in one place, basically it's in the “RouteConfig.cs” file.

It's great to keep all the routing information in one place to make it centralized. But the problem is that if we declare a routing mechanism in this way, then all the actions and the controller will follow the same routing technique. Now if we wanted to define a different routing to a different controller then we need to implement attribute routing.

Using attribute routing we can define various routing techniques for different controllers, so the user will get more freedom to use the application.

Since this is called attribute routing, we will use the Route() attribute to the action. In this example, we defined two actions and we have specified a route above this.

The first products() action will execute when we browse to the following URL.


And if we want to invoke a second action then we need to browse to the following URL.


Now, to enable attribute routing we need to enable it in the Route.Config.cs file. Just add the following single line of code.

enable attribute routing

Here is sample code to execute.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. using MVC_5.App_Data;  
  8. namespace MVC_5.Controllers  
  9. {  
  10.     public class testController : Controller  
  11.     {  
  12.         [Route("products/shirt")]  
  13.         public void products()  
  14.         {  
  15.         }  
  16.         [Route("products/shirt/{id}")]  
  17.         public void products(int id)  
  18.         {  
  19.         }  
  20.     }  
  21. }

Here is sample output of when we are browsing.


And we are seeing that it has stuck to the second action.


Pass optional parameter

We can combine two actions into one if we make the parameter as an optional parameter. Here is sample code.

  1. public class testController : Controller  
  2. {  
  3.     [Route("products/shirt/{id?}")]  
  4.     public void products(int? id)  
  5.     {  
  6.     }  
  7. }

Now, this action will get everything for the preceding URLs.


Define Route constraints

We can specify parameters constraints placing the constraint name after the parameter name separated by a colon.

  1. [Route("products/shirt/{id:int}")]  
  2. public void products(int id)  
  3. {  
  4. }

In the example above we have defined id as an integer type. It needs to be able to define any data type. Here I have given a few examples.


Matches a bool parameter.


Matches a string parameter with a maximum length of n characters.


Matches a string parameter with a minimum length of n characters


Matches an integer parameter with a maximum value of n.


Matches an integer parameter with a minimum value of n.


Matches an integer parameter within a range of values.


Matches a floating-point parameter.


Matches uppercase or lowercase characters.


Matches a regular expression.


Matches a DateTime parameter.


We have seen how attribute routing works in MVC 5. Attribute routing is very useful when the routing mechanism is not fixed for all controllers.

Similar Articles