Search code examples
asp.net-web-apihttp-status-codeshttp-status-code-405

ASP.NET Web API, 405 status code and exception handling


Is there a way to catch a 405 on Web API and return a custom message when the wrong Verb is sent? I am catching 500's by using ExceptionFilterAttribute. I am wanting to return a JSON or XML object.


Solution

  • HttpStatusCode.MethodNotAllowed is your friend.

    http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx

    Check Request.StatusCode for it in your filter.