는 I은이 URL에 POST를 받아들이는 컨트롤러가 :ASP.NET MVC 라우팅에서 HttpMethodConstraint와 함께 사용자 정의 제약 조건을 사용하려면 어떻게해야합니까?
POST http://server/stores/123/products
POST는 내용 유형 application/json
이어야한다, 그래서 이것은 내 라우팅 테이블에있는 것입니다 :
routes.MapRoute(null,
"stores/{storeId}/products",
new { controller = "Store", action = "Save" },
new {
httpMethod = new HttpMethodConstraint("POST"),
json = new JsonConstraint()
}
);
어디 JsonConstraint
것은 : 내가 경로를 사용하는 경우
public class JsonConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.ContentType == "application/json";
}
}
, 나는 금지 된 405를 얻을 :
The HTTP verb POST used to access path '/stores/123/products' is not allowed
그러나 json = new JsonConstraint()
제약 조건을 제거하면 올바르게 작동합니다. 아무도 내가 뭘 잘못하고 있는지 알아?
당신이 jQuery를 조각을 게시 할 수 있습니다하세요? 추가 테스트를 실행했으며 ContentType이 "application/xml"으로 표시되고 있음을 보여줍니다. –
나는 이것을 테스트하기 위해'REST Client'라는 Firefox 플러그인을 사용하고 있습니다. –