In ASP.NET MVC applications routes are used to map the URL which browser requests with the action methods and controllers.This mapping is the responsibility of the routing system.The routes are defined in the RouteConfig.cs file in the App_Start folder of the application.
If we look in the file this is the default route defined
public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
The above route will map the requested URL with the controller and action methods and will use the default if none of the values are specified in the URL.
So the URL pattern in the route maps the request to the action method and controller.But what if we want to further restrict the matched URL by defining additional matching conditions.We can do so using the route constraints in MVC.
We will add a new route called “HomePage” which will match the controller and action methods to the URL.
routes.MapRoute( name: "HomePage", url: "{controller}/{action}", defaults: new { controller = "HomePage", action = "Welcome" }, constraints: new { action="^Welcome$"}
If we run the application we will get the expected response in the browser.
But now if we change the last segment in the URL to “x” and make the request we will get the following response in the browser.
This is because we have not defined any action method called “x” in the HomePageController class.Our HomePageController class only defines the Welcome action method so far.This is where constraints are used in routing.We can restrict the URL’s from matching our routes using constraints.Constraints are defined in an anonymous type as a parameter to the MapRoute() method.
We can prevent the URL’s which consists of non-existing action methods from matching this route using constraints
routes.MapRoute( name: "HomePage", url: "{controller}/{action}", defaults: new { controller = "HomePage", action = "Welcome" }, constraints: new { action="^Welcome$"} );
The above constraint will restrict the URL’s to match our route only when the action segment variable contains the value “Welcome”.
We can also separate multiple values using the “|” character as:
action="^Welcome$|^Details$"
Instead of defining a specific constraint value for a segment we can also use regular expressions so any pattern matching the expression will satisfy the constraint.For example the following will match any url in which action segment starts with “H”
action= “^H.*”
There are also constraint classes using which we can check if a segment variable is of specific data type.The common constraint classes are:
Constraint | Description |
DoubleRouteConstraint() | Matches if segment value can be converted to a double |
FloatRouteConstraint() | Matches if segment value can be converted to a float |
IntRouteConstraint() | Matches if segment value can be converted to a int |
RangeRouteConstraint(min, max) | Matches if segment value can be converted to a int and lies between the specified range |
Leave a Reply