HI WELCOME TO KANSIRIS

Route Constraints in Asp.Net MVC with example

In previous article, I have described the Routing and how to create route in your application. Now the time is how to control the behavior of a route. Basically route constraints is way to put some validation around the defined route.

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.
  1. routes.MapRoute(
  2. "Default", // Route name
  3. "{controller}/{action}/{id}", // Route Pattern
  4. new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default values for parameters
  5. );

Restrict to numeric id only

  1. routes.MapRoute(
  2. "Default", // Route name
  3. "{controller}/{action}/{id}", // Route Pattern
  4. new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
  5. new { id = @"\d+" } //Restriction for id
  6. );
Now for this route, routing engine will consider only those URLs which have only numeric id like as http://example.com/Admin/Product/1 else it will considers 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 controller name with H prefix and action name should be only Index or Contact. Now let's see how to do it with the help of regular expression.
  1. routes.MapRoute(
  2. "Default", // Route name
  3. "{controller}/{action}/{id}", // Route Pattern
  4. new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
  5. new { controller = "^H.*", action = "^Index$|^Contact$" } //Restriction for controller and action
  6. );
Now for this route, routing engine will consider only those URLs which have controller name with H prefix and 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 considers that url is not matched with this route.
You are little bit confused why it will consider the http://example.com,http://example.com/Home URLs ?. It will also consider both these since route constraints is checked after the provided default values for controller and action. In 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 route on the top order while defining the routes, since routing system check the incoming URL pattern form the top and as it get the matched route it will consider that. It will not checked further routes after matching pattern.
What do you think?
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, question, or comments about this article are always welcome.