Reputation: 165
As of now, the Web API application returns the below response body for 405 - Method Not Allowed error. I am trying to change the response body, but I don't know how the delegating handler, ApiControllerActionSelector
or filter can be used. Can anyone help me on catching the 405 error in server side?
{
message: "The requested resource does not support http method 'GET'."
}
Note: My API controllers has [RoutePrefix]
value.
Upvotes: 2
Views: 949
Reputation: 2145
You could use a DelegatingHandler to catch the outgoing response and override its behaviour like this.
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;
}
}
In Global.asax...
config.MessageHandlers.Add(new MethodNotAllowedDelegatingHandler());
Upvotes: 5