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 constrains are given below that we can use with the Route attribute.
Route Constrain |
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:
public class HomeController : Controller
{
// URL: /Mvctest/1
[Route(“Mvctest /{ customerId:int}”)]
public ActionResult GetCutomerById(int customerId)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
// URL: /Mvctest/{customerName}
[Route(“Mvctest /{ customerName}”)]
public ActionResult GetCutomerByName(string customerName)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}
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.
// URL: /Mvctest/{customerName:maxlenth(20)}
[Route(“Mvctest /{ customerName}”)]
public ActionResult GetCutomerByName(string customerName)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
Defining multiple constraints
We can also apply multiple constrains to the single route to get more control over the URLs with a route. We can apply the multiple constrains to a parameter by a colon (:) separator.
Syntax
[Route(URLPath/{parameterName: constrain:Constrain:….})]
Example
// URL: /Mvctest/1 à Action method is not be selected
// URL: /Mvctest/1001 à Action method is selected
[Route(“Mvctest /{ customerId:int:min(1000)}”)]
public ActionResult GetCutomerById(int customerId)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
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
// URL: /Mvctest/
// URL: /Mvctest/1
[Route(“Mvctest /{ customerId:int?}”)]
public ActionResult GetCutomerById(int customerId)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
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.