HTTP 405 error with Web API

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

2 Comments

  • In fact this is an issue which came up in general with windows 10 and IIS 10 (correct me, if the version is wrong).
    So it's not only IISExpress but a general "problem"

  • Yes Andreas, it is a general problem. But the issue didn't come up with IIS Express. Normally the list of handlers and modules are different between the development (IIS Express) and UAT/Production environments (IIS). IIS Express is a scaled down version of the complete IIS. IIS Express installs the WebDAV, but the entries are commented out in applicationhost.config and it is not loaded. So the Web API works correctly on IIS Express and not when it got published to the server. Hope this answers your comment.

Comments have been disabled for this content.