截至目前,Web api应用程序返回以下响应正文405 - Method Not Allowed错误。我试图更改响应体,但我不知道如何使用委托处理程序,ApiControllerActionSelector或过滤器。任何人都可以帮我解决服务器端的405错误吗?
{
message: "The requested resource does not support http method 'GET'."
}
注意:我的api控制器有[RoutePrefix]值。
答案 0 :(得分:5)
您可以使用DelegatingHandler来捕获传出响应并覆盖其行为。
public class MethodNotAllowedDelegatingHandler : DelegatingHandler
{
async protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == HttpStatusCode.MethodNotAllowed)
{
// adjust your response here as needed...
}
return response;
}
}
在Global.asax中......
config.MessageHandlers.Add(new MethodNotAllowedDelegatingHandler());