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 ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.