HI WELCOME TO KANSIRIS

Routing Constrains With Attribute Routing in MVC 5.0

Leave a Comment
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 ConstrainUsed ForExample
AlphabateIt matches uppercase or lowercase Latin alphabet characters (a-z and A-Z) values.{ParameterName:alpha}
DateTimeIt matches a Date Time value.{ParameterName:datetime}
DecimalIt matches a decimal value.{ParameterName:decimal}
FloatIt matches a 32-bit floating-point value.{ParameterName:float}
IntegerIt matches a 32-bit integer value.{ParameterName:int}
LongIt matches a 64-bit integer value.{ParameterName:long}
DoubleIt matches a 64-bit floating-point value.{ParameterName:double}
MaxIt matches an integer with a maximum value.{ParameterName:max(100)}
MinIt matches an integer with a minimum value.{ParameterName:min(5)}
LengthIt matches a string with the specified length or within a specified range of lengths.{ParameterName:length(20)} {ParameterName:length(3,20)}
MaxLengthIt matches an string with a maxmum value.{ParameterName:maxlength(15)}
MinLengthIt matches a string with a minimum length.{ParameterName:minlength(6)}
BooleanIt matches a boolean value{ParameterName:bool}
RangeIt matches an integer within a range of values.{ParameterName:range(1,500)}
RegexIt matches a regular expression.{ParameterName:(^[a-zA-Z0-9_]*$)} {ParameterName:(^\d{3}-\d{5}-$)}
GUIDIt 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.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.