Browse Tutorials

02 Intermediate

Route Constraints in Asp.Net MVC with example

Route Constraints in Asp.Net MVC with example

01 Apr 2024
Intermediate
176K Views
5 min read

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

There are two types of route constraints
  • Predefined Route Constraints
  • Custom Route Constraints

 A. Predefined Route Constraints

 1. IntConstraint

IntConstraint matches a 32-bit integer value. 

Syntax:

{ParameterName: int}

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

Conclusion:
I hope you have got what is routing and how it works. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome.

FAQs

Q1. What is route constraints in ASP.NET MVC?

To restrict the browser requests that match a particular route.

Q2. What is routing in MVC with example?

Routing is a screening process that monitors applications and determines what to do with each application

Q3. How many types of routing are there in MVC?

There are two types of routing for action methods: Conventional Routing. Attribute Routing.
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this