我正在尝试按照此处描述的步骤实现 HTTP 方法覆盖。基本上,我正在创建一个 DelegatingHandler,类似于以下内容,并将其作为消息处理程序添加到Application_Start
.
public class MethodOverrideHandler : DelegatingHandler
{
readonly string[] _methods = { "DELETE", "HEAD", "PUT" };
const string _header = "X-HTTP-Method-Override";
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
// Check for HTTP POST with the X-HTTP-Method-Override header.
if (request.Method == HttpMethod.Post && request.Headers.Contains(_header))
{
// Check if the header value is in our methods list.
var method = request.Headers.GetValues(_header).FirstOrDefault();
if (_methods.Contains(method, StringComparer.InvariantCultureIgnoreCase))
{
// Change the request method.
request.Method = new HttpMethod(method);
}
}
return base.SendAsync(request, cancellationToken);
}
}
我在控制器上定义了以下方法:
persons/{id}
, 得到persons/{id}
, 放persons/{id}
, 删除
我可以通过他们的“本机”方法调用它们,并且它们按预期工作。但是,当我尝试通过 POST 调用它们时,发送X-HTTP-Method-Override
带有“DELETE”或“PUT”的标头时,会出现Not Found (404)错误。重要的是要补充一点,当它给出这个错误时,它永远不会到达MethodOverrideHandler
- 我已经放置了一个永远不会命中的断点;当我调用正常的 DELETE 和 PUT 时,它确实达到了断点。
我什至尝试添加另一种方法:
persons/{id}
, 邮政
当我这样做时,我得到了一个不允许的方法(405)。
我认为消息处理程序是在路由和控制器调度程序之前运行的。为什么这给了我404?
我不认为这是相关的,但我没有使用默认的 Web API 路由。相反,我使用分配给每个方法的自定义属性进行映射,如下所示:
routes.MapHttpRoute(
String.Format("{0}_{1}", operation.Name, service.ServiceId),
String.Format("{0}/{1}", service.RoutePrefix, routeTemplateAttribute.Template),
defaults,
new { httpMethod = GetHttpMethodConstraint(operation) });
[HttpDelete, RouteTemplate("persons/{id}")]
public HttpResponseMessage DeletePerson(string id)
{
// ...
}
编辑:GetHttpMethodConstraint
代码如下。
private static HttpMethodConstraint GetHttpMethodConstraint(MethodInfo methodInfo)
{
var methodResolver = HttpMethodResolver.FromMethodInfo(methodInfo);
return new HttpMethodConstraint(methodResolver.Resolve());
}
internal class HttpMethodResolver
{
private MethodInfo _methodInfo;
private HttpMethodResolver(MethodInfo methodInfo)
{
_methodInfo = methodInfo;
}
public static HttpMethodResolver FromMethodInfo(MethodInfo methodInfo)
{
return new HttpMethodResolver(methodInfo);
}
public string[] Resolve()
{
var verbs = new List<HttpMethod>();
if (MethodHasAttribute<HttpGetAttribute>())
{
verbs.Add(HttpMethod.Get);
}
else if (MethodHasAttribute<HttpPostAttribute>())
{
verbs.Add(HttpMethod.Post);
}
else if (MethodHasAttribute<HttpDeleteAttribute>())
{
verbs.Add(HttpMethod.Delete);
}
else if (MethodHasAttribute<HttpPutAttribute>())
{
verbs.Add(HttpMethod.Put);
}
else
{
throw new ServiceModelException("HTTP method attribute should be used");
}
return verbs.Select(v => v.Method).ToArray();
}
private bool MethodHasAttribute<T>() where T : Attribute
{
return GetMethodAttribute<T>() != null;
}
private T GetMethodAttribute<T>() where T : Attribute
{
return _methodInfo.GetCustomAttributes(typeof(T), true).FirstOrDefault() as T;
}
}