In this article, you will learn about route constraints in ASP.NET MVC C#. Routing plays a vital role in the ASP.NET MVC web applications.
In this article, we will discuss
By the end of this article, you have a basic understanding of the Route Constraints in ASP.NET MVC.
In ASP.NET MVC, Route Constraints helps you to restrict the browser requests that match a particular route. You can use a regular expression to specify a route constraint. In other words, we say that Route constraint is a way to put some validation around the defined route for a controller action.
For Example, take a look at the Default Route.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Default Route
routes.MapRoute(
name: "Default", url: "{controller}/{action}/{id}", defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
Now, if you want to restrict incoming request URLs with numeric id only. Let's see how we can achieve this using regular expressions.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Default Route
routes.MapRoute(
name: "Default", url: "{controller}/{action}/{id}", defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
constraints: new {
id = @"\d+"
} //this restrict the ID parameter to Numeric only
);
}
}
In the above example, if you give a non-integer value for the id parameter then that request will be handled by another route or if there are no matching routes will be found then the “The resource could not be found” error will be thrown. So now for the above route, the routing engine will only consider the URLs which have only numeric id like https://www.example.com/Home/Index/15
So if you want to allow a specific controller or specific action to execute. Then, you need to make a small change in the Route entry. Here is the modified root entry.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Default Route
routes.MapRoute(
name: "Default", url: "{controller}/{action}/{id}", defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
//Restriction for controller and action
constraints: new {
controller = "^H.*",
action = "^Contact$|^About$"
});
}
}
Using the above route example, you will allow only those controllers whose name starts with “H” and only “About” or “Contact” actions are allowed to execute. If you try to execute another controller and action, it will give you the error “HTTP Error 404.0 - Not Found”.
So, only the following combination will satisfy.
http://www.example.com/Home/About
or
http://www.example.com/Home/Contact
If you try to execute this URL, “http://www.example.com/Home/index” then, it will throw an error “HTTP Error 404.0 - Not Found”.
I hope this article will help you to understand the basic concepts of the route constraints in ASP.NET MVC.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments