Custom Route Constraints

The “Route Constraints” section earlier in this chapter covered how to use regular expressions to provide fine-grained control over route matching. As you might recall, we pointed out that the RouteValueDictionary class is a dictionary of string-object pairs. When you pass in a string as a constraint, the Route class interprets the string as a regular expression constraint. However, it is possible to pass in constraints other than regular expression strings.

Routing provides an IRouteConstraint interface with a single Match method. Here's a look at the interface definition:

public interface IRouteConstraint
{
  bool Match(HttpContextBase httpContext, Route route, string parameterName, 
    RouteValueDictionary values, RouteDirection routeDirection);
}

When Routing evaluates route constraints, and a constraint value implements IRouteConstraint, it will cause the route engine to call the IRouteConstraint.Match method on that route constraint to determine whether or not the constraint is satisfied for a given request.

Route constraints are run for both incoming URLs and while generating URLs. A custom route constraint will often need to inspect the routeDirection parameter of the Match method to apply different logic depending on when it is being called.

Routing itself provides one implementation of this interface in the form of the HttpMethodConstraint class. This constraint allows you to specify that a route should match only requests that use a specific set of HTTP methods (verbs).

For example, if you want a route to respond only to GET requests, but not POST, PUT, or DELETE requests, you could define the following route:

routes.MapRoute("name", "{controller}", null
 , new {httpMethod = new HttpMethodConstraint("GET")} );

Note
Custom constraints don't have to correspond to a URL parameter. Thus, it is possible to provide a constraint that is based on some other piece of information, such as the request header (as in this case), or on multiple URL parameters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset