HTTP 405 error with Web API

Datetime:2016-08-22 22:59:07          Topic:          Share

One of the developer in my team ran into a problem recently with the HTTP verb “PUT” and "DELETE" + ASP.NET Web API.  We are using Visual Studio 2015 to develop the Web API and IIS Express is the default web server for testing.  The Web API was working fine during the testing and started giving HTTP 405 errors when we published the Web API to the server.

What is the Problem?

The primary reason for this issue is multiple handlers are defined for the same HTTP verb and one of the handlers is blocking the expected handler from processing the request. IIS processes the handlers in the order they entered in “ applicationhost.config ”.  This would be one of the first place you have to check for a HTTP 405 error.  In our case it was WebDAV which blocked the “ExtensionlessUrlHandler” from processing the request which has PUT verb.  PUT and DELETE are considered WebDAV verb, and webdav module claims the request , but it returns 405 since webdav is disabled in the server.

There are couple of solutions available for this problem. But most of them require modifications in the IIS Manager or applicationhost.config file.  It would be difficult if a shared server is being used to deploy more than one application and changes made for the current application may cause some issues to the other applications deployed in the same server. We preferred to solve this issue by doing modifications in the Web API’s web.config.

Solution

When you create a Web API project, you get the following by default in web.config.

<system.webServer>
	<handlers>
		<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>
	<validation validateIntegratedModeConfiguration="false" />
	<modules>
		<remove name="ApplicationInsightsWebTracking" />
		<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
		</modules>
</system.webServer>

Just add the following remove statements to your web.config.

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

Other Solutions:

Uninstall WebDAV

Modify applicationhost.config

Use X-HTTP-Method-Override