【发布时间】:2010-01-28 23:26:12
【问题描述】:
我有一个只接受这个 URL 上的 POST 的控制器:
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 Forbidden:
The HTTP verb POST used to access path '/stores/123/products' is not allowed
但是,如果我删除 json = new JsonConstraint() 约束,它可以正常工作。有人知道我做错了什么吗?
【问题讨论】:
-
请问您能发布您的 jQuery sn-p 吗?我已经运行了一些进一步的测试,它显示 ContentType 显示为“application/xml”。
-
我正在使用一个名为
REST Client的 Firefox 插件来测试它。
标签: asp.net-mvc routing asp.net-mvc-routing