Monday, December 29, 2014

Troubleshooting "Method Not Allowed" errors in ASP.NET Web API

If you are working with ASP.NET Web API, you may have encountered a situation whereby you can perform a GET or POST just fine with your ASP.NET Web API project, but cannot successfully execute a PUT without getting a "HTTP/1.1 405 Method Not Allowed" error message even when you have set up the HttpPut verb on your Put method in your Web API controller.

Well, there are 2 possible solutions to this problem:

  1. The request for PUT could be a Cross-Origin Request and thus could be blocked automatically by Web API.  In order to add CORS support to your ASP.NET Web API application, you can follow the steps outlined here: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 
  2. The other potential issue could be your configuration of IIS.  The WebDAV module in IIS could potentially conflict with PUT requests, in which case you may have to modify your Web.config file as outlined in this article: http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

However, if like me, neither of these solutions worked for you, you may need to additionally remove the WebDAV module from IIS using the following Web.config section:

<modules>
    <remove name="WebDAVModule" />
</modules>

Therefore, your system.WebServer section should end up looking like the following:



<system.webServer>
  <handlers>
    <remove name="WebDAV" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
  <modules>
    <remove name="WebDAVModule" />
  </modules>
</system.webServer>

No comments:

Post a Comment