我正在寻找这个问题的答案,发现这个问题确实非常相似。但是,那里发布的解决方案似乎对我不起作用……我想知道这是否与问题的年龄有关。
给定以下网址:
/my/items/6
我希望此 URL 的 HTTP PUT 请求由一种操作方法处理,而 HTTP DELETE 请求由另一种操作方法处理。以下是我定义的路线(请注意,这些路线是基于一个区域,如果重要的话,context
也是一个实例):AreaRegistrationContext
context.MapRoute(null,
"my/items/{id}",
new { area = "AreaName", controller = "ControllerName", action = "Replace" },
new
{
httpMethod = new HttpMethodConstraint("POST", "PUT"),
}
);
context.MapRoute(null,
"my/items/{id}",
new { area = "AreaName", controller = "ControllerName", action = "Destroy" },
new
{
httpMethod = new HttpMethodConstraint("POST", "DELETE"),
}
);
URL 生成适用于这两种路由,但是在路由传入请求时会出现问题。只有第一个声明的路由正确映射到其各自的操作。
我深入研究了HttpMethodConstraint
源代码,发现它不关心"X-HTTP-Method-Override"
参数,只关心HttpContext.Request.HttpMethod
.
我能够使用以下自定义路由约束类解决此问题:
public class HttpMethodOverrideConstraint : HttpMethodConstraint
{
public HttpMethodOverrideConstraint(params string[] allowedMethods)
: base(allowedMethods) { }
protected override bool Match(HttpContextBase httpContext, Route route,
string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
var methodOverride = httpContext.Request
.Unvalidated().Form["X-HTTP-Method-Override"];
if (methodOverride == null)
return base.Match(httpContext, route, parameterName,
values, routeDirection);
return
AllowedMethods.Any(m =>
string.Equals(m, httpContext.Request.HttpMethod,
StringComparison.OrdinalIgnoreCase))
&&
AllowedMethods.Any(m =>
string.Equals(m, methodOverride,
StringComparison.OrdinalIgnoreCase))
;
}
}
...以及这些路线定义:
context.MapRoute(null,
"my/items/{id}",
new { area = "AreaName", controller = "ControllerName", action = "Replace" },
new
{
httpMethod = new HttpMethodOverrideConstraint("POST", "PUT"),
}
);
context.MapRoute(null,
"my/items/{id}",
new { area = "AreaName", controller = "ControllerName", action = "Destroy" },
new
{
httpMethod = new HttpMethodOverrideConstraint("POST", "DELETE"),
}
);
我的问题:真的有必要有一个自定义路由约束来完成这个吗?或者有什么方法可以使其与标准 MVC 和路由类开箱即用?