Routing Constrains With Attribute Routing in MVC 5.0

Introduction

Attribute Routing is introduced in MVC 5.0. We can also define parameter constraints by placing a constraint name after the parameter name separated by a colon.

This is very similar to function/method overriding; a route will only match if the data type of the parameter matches, otherwise the request falls to the next matching route. Route constraints will restrict how the parameters in the route attribute are matched.

Syntax

[Route(URLPath/{parameterName: constrain})]

Some of the useful constraints are given below that we can use with the Route attribute.

Route Constraint Used For Example
Alphabate It matches uppercase or lowercase Latin alphabet characters (a-z and A-Z) values. {ParameterName:alpha}
DateTime It matches a Date Time value. {ParameterName:datetime}
Decimal It matches a decimal value. {ParameterName:decimal}
Float It matches a 32-bit floating-point value. {ParameterName:float}
Integer It matches a 32-bit integer value. {ParameterName:int}
Long It matches a 64-bit integer value. {ParameterName:long}
Double It matches a 64-bit floating-point value. {ParameterName:double}
Max It matches an integer with a maximum value. {ParameterName:max(100)}
Min It matches an integer with a minimum value. {ParameterName:min(5)}
Length It matches a string with the specified length or within a specified range of lengths. {ParameterName:length(20)} {ParameterName:length(3,20)}
MaxLength It matches an string with a maxmum value. {ParameterName:maxlength(15)}
MinLength It matches a string with a minimum length. {ParameterName:minlength(6)}
Boolean It matches a boolean value {ParameterName:bool}
Range It matches an integer within a range of values. {ParameterName:range(1,500)}
Regex It matches a regular expression. {ParameterName:(^[a-zA-Z0-9_]*$)} {ParameterName:(^\d{3}-\d{5}-$)}
GUID It matches a GUID value. {ParameterName:guid}

Practical Example

The following is a practical example:

  1. public class HomeController : Controller  
  2. {  
  3.   
  4.   // URL: /Mvctest/1  
  5.    [Route(“Mvctest /{ customerId:int}”)]  
  6.     public ActionResult GetCutomerById(int customerId)  
  7.     {  
  8.         ViewBag.Message = "Welcome to ASP.NET MVC!";  
  9.         return View();  
  10.     }  
  11.   
  12.   // URL: /Mvctest/{customerName}  
  13.     [Route(“Mvctest /{ customerName}”)]  
  14.     public ActionResult GetCutomerByName(string customerName)  
  15.     {  
  16.         ViewBag.Message = "Welcome to ASP.NET MVC!";  
  17.         return View();  
  18.     }  
  19. }  

In the preceding example, the route will only be selected action method if the id segment URL is an integer otherwise it will select a second one.

Some of constrains (like min, max, length and so on) take an argument in parentheses.

  1. // URL: /Mvctest/{customerName:maxlenth(20)}  
  2. [Route(“Mvctest /{ customerName}”)]  
  3. public ActionResult GetCutomerByName(string customerName)  
  4. {  
  5.     ViewBag.Message = "Welcome to ASP.NET MVC!";  
  6.     return View();  
  7. }   

Defining multiple constraints


We can also apply multiple constraints to the single route to get more control over the URLs with a route. We can apply the multiple constraints to a parameter by a colon (:) separator.

Syntax

[Route(URLPath/{parameterName: constrain:Constrain:….})]

Example

  1. // URL: /Mvctest/1 à Action method is not be selected  
  2. // URL: /Mvctest/1001 à Action method is selected  
  3.  [Route(“Mvctest /{ customerId:int:min(1000)}”)]  
  4.  public ActionResult GetCutomerById(int customerId)  
  5.  {  
  6.      ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.      return View();  
  8.  }  

In the preceding example, the route will only be selected if the parameter id is an integer as well as a value of id greater than 1000.

Defining Route constrain with Optional Parameter


We can also define an optional parameter in a URL pattern by defining a question mark (“?") to the route parameter.

Example

  1. // URL: /Mvctest/  
  2. // URL: /Mvctest/1  
  3. [Route(“Mvctest /{ customerId:int?}”)]  
  4. public ActionResult GetCutomerById(int customerId)  
  5. {  
  6.     ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.      return View();  
  8. }  

Summary

We can also define a constraint based routing on the action method by using the Route attribute. In constraint based routing a route will only be selected if the data type is matched. We can also define a custom route constraint.


Similar Articles