Route Constraints in Asp.Net MVC with example
Route Constraints: An Overview
In the previous article, I have described routing and how to create route in your application. Now the time is how to control the behavior of a route. Basically, route constraints are the way to put some validation around the defined route. In this MVC Tutorial, we will explore more about Route Constraints which will include creating route constraints in asp.net mvc and understanding route constraints. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.
Creating Route Constraints
Suppose we have defined the following route in our application and you want to restrict the incoming request url with numeric id only.Now let's see how to do it with the help of regular expression.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // Route Pattern
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default values for parameters
);
Restrict to numeric ID only
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // Route Pattern
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
new { id = @"\d+" } //Restriction for id
);
Now for this route, the routing engine will consider only those URLs that have only numeric ID like as http://example.com/Admin/Product/1 else it will consider that URL is not matched with this route.
Creating Route Constraint to a Set of Specific Values
Suppose you want to restrict the user for those URLs that have a controller name with an H prefix and the action name should be only Index or Contact. Now let's see how to do it with the help of regular expression.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // Route Pattern
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
new { controller = "^H.*", action = "^Index$|^Contact$" } //Restriction for controller and action
);
Now for this route, the routing engine will consider only those URLs which have a controller name with an H prefix and the action name should be only Index or Contact like as http://example.com/Home/Index, http://example.com/Home/Contact and http://example.com,http://example.com/Home else it will consider that URL is not matched with this route.
You are a little bit confused as to why it will consider the http://example.com,http://example.com/Home URLs ?. It will also consider both these since route constraints are checked after the provided default values for controller and action. In the above route default values for controller and action are Home and Index so these two URLs will also be matched. Like this you can restrict the user according to your need.
Note
Always put more specific routes on the top order while defining the routes, since the routing system checks the incoming URL pattern from the top and as it gets the matched route it will consider that. It will not check further routes after the matching pattern.
Types of Route Constraints
- Predefined Route Constraints
- Custom Route Constraints
A. Predefined Route Constraints
1. IntConstraint
IntConstraint matches a 32-bit integer value.
Syntax:
2. BoolConstraint
Syntax
3. DateTimeConstraint
Syntax
4. DecimalConstraint
Syntax
5. DoubleConstraint
Syntax
6. FloatConstraint
Syntax
7. GuidConstraint
Syntax
8. LongConstraint
Syntax
9. MaxLengthRouteConstraint
Syntax
10. MinLengthRouteConstraint
Syntax
11. RangeRouteConstraint
Syntax
B. Custom Route Constraints
1. Implementation Steps
H4 2. Advantages of Custom Route Constraints