I have a controller that only accepts a POST on this URL:
POST http://server/stores/123/products
The POST should be of content-type application/json
, so this is what I have in my routing table:
routes.MapRoute(null,
"stores/{storeId}/products",
new { controller = "Store", action = "Save" },
new {
httpMethod = new HttpMethodConstraint("POST"),
json = new JsonConstraint()
}
);
Where JsonConstraint
is:
public class JsonConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.ContentType == "application/json";
}
}
When I use the route, I get a 405 Forbidden:
The HTTP verb POST used to access path '/stores/123/products' is not allowed
However, if I remove the json = new JsonConstraint()
constraint, it works fine. Does anybody know what I'm doing wrong?