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.

http://localhost:2968/products/shirt

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

http://localhost:2968/products/shirt/10

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.

http://localhost:2968/products/shirt/10

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

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.

http://localhost:2968/products/shirt
http://localhost:2968/products/shirt/10

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.

x:bool

Matches a bool parameter.

x:maxlength(n)

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

x:minlength(n)

Matches a string parameter with a minimum length of n characters

x:max

Matches an integer parameter with a maximum value of n.

x:min

Matches an integer parameter with a minimum value of n.

x:range

Matches an integer parameter within a range of values.

x:float

Matches a floating-point parameter.

x:alpha

Matches uppercase or lowercase characters.

x:regex

Matches a regular expression.

x:datetime

Matches a DateTime parameter.

Conclusion

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