I am trying to test constraint usage with the new "IgnoreRoute" extension. I have the following setup:
In the following example (i would like to ignore a request based on a custom constraint...right, the constraint looks contrived, but i just want to see if constraints work fine here or not)
Attached a webhost project having repro
```
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.Routes.IgnoreRoute("IgnoreApiVariable", "api/values/{id}", constraints: new { cc = new CustomConstraint() });
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class CustomConstraint : IHttpRouteConstraint
{
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName,
IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
long id;
if(values.ContainsKey("id")
&& Int64.TryParse(values["id"].ToString(), out id)
&& (id == 10 || id == 15 || id == 20))
{
return true;
}
return false;
}
}
```
_Exception_:
```
The constraint entry 'cc' on the route with URL 'api/values/{id}' must have a string value or be of a type which implements IRouteConstraint.
[InvalidOperationException: The constraint entry 'cc' on the route with URL 'api/values/{id}' must have a string value or be of a type which implements IRouteConstraint.]
System.Web.Routing.Route.ProcessConstraint(HttpContextBase httpContext, Object constraint, String parameterName, RouteValueDictionary values, RouteDirection routeDirection) +2439879
System.Web.Routing.Route.ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection) +99
System.Web.Routing.Route.GetRouteData(HttpContextBase httpContext) +176
System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) +233
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +60
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
```
In the following example (i would like to ignore a request based on a custom constraint...right, the constraint looks contrived, but i just want to see if constraints work fine here or not)
Attached a webhost project having repro
```
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.Routes.IgnoreRoute("IgnoreApiVariable", "api/values/{id}", constraints: new { cc = new CustomConstraint() });
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class CustomConstraint : IHttpRouteConstraint
{
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName,
IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
long id;
if(values.ContainsKey("id")
&& Int64.TryParse(values["id"].ToString(), out id)
&& (id == 10 || id == 15 || id == 20))
{
return true;
}
return false;
}
}
```
_Exception_:
```
The constraint entry 'cc' on the route with URL 'api/values/{id}' must have a string value or be of a type which implements IRouteConstraint.
[InvalidOperationException: The constraint entry 'cc' on the route with URL 'api/values/{id}' must have a string value or be of a type which implements IRouteConstraint.]
System.Web.Routing.Route.ProcessConstraint(HttpContextBase httpContext, Object constraint, String parameterName, RouteValueDictionary values, RouteDirection routeDirection) +2439879
System.Web.Routing.Route.ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection) +99
System.Web.Routing.Route.GetRouteData(HttpContextBase httpContext) +176
System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) +233
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +60
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
```