ASP.NET Web API - PUT & DELETE Verbs Not Allowed - IIS 8
Okay. I finally got to the bottom of this. You need to jump through some hoops to get the PUT and DELETE verbs working correctly with IIS8. In fact if you install the release candidate of VS 2012 and create a new WEB API project you'll find that the sample PUT and DELETE methods return 404 errors out of the box.
To use the PUT and DELETE verbs with the Web API you need to edit %userprofile%\documents\iisexpress\config\applicationhost.config and add the verbs to the ExtensionlessUrl handler as follows:
Change this line:
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
to:
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
In addition to the above you should ensure WebDAV is not interfering with your requests. This can be done by commenting out the following lines from applicationhost.config.
<add name="WebDAVModule" image="%IIS_BIN%\webdav.dll" />
<add name="WebDAVModule" />
<add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />
Also be aware that the default Web API convention is that your method name should be the same as the invoked HTTP verb. For example if you're sending an HTTP delete request your method, by default, should be named Delete.
Asp.net MVC Web Api Http put and delete requests failing
I tried all the suggestions from the comment above and none of them worked.
I had to add the ASP.NET 4.0 dll to the Wildcard mappings in the Configuration area on the Home Directory tab. That worked for me. Remember to uncheck the "Verify file exists" checkbox.
EDIT: I posted a blog on the exact process to make this happen here: http://www.proworks.com/blog/2012/11/14/how-to-fix-aspnet-mvc-web-api-http-put-and-delete-requests-failing/
Enable Http PUT requests on .NET MVC
After much fruitless searching and blind alleys involving WebDAV I found the answer on another SO family site :)
https://serverfault.com/questions/93424/how-to-enable-put-and-delete-in-iis7
ASP.NET Core with IIS - HTTP Verb Not Allowed
Whilst removing WebDAV may solve your issue, you may be wondering (like I was), what is WebDAV and what is it used for?
I found this page which helps explain it:
https://www.cloudwards.net/what-is-webdav/
HTTP Verbs PUT and DELETE: 405 Method not allowed - how to allow?
A day after, I now found the cause for this error. The following reasons caused this error to happen:
- There was a typo in the PUT call, so the Web API method wasn't called. After fixing that, the following two issues had to be fixed:
- It wasn't sufficient to prefix the Web API methods with the method and omit the corresponding attributes (
[HttpPut]
,[HttpDelete]
). So, these attributes had to be applied (this is particular to ASP.NET Core). - It wasn't sufficient to provide the above attributes to the methods. It was required to provide the methods' address (and query string, resp.) parameters for these attributes (
[HttpPut("{id}")]
,[HttpDelete("id")]
), too. (This is particular to ASP.NET Core.)
See ASP.NET Core Web API: Routing by method name? .
I believe "405 Method not allowed" to be quite an inappropriate error message. It doesn't reflect any of the above three reasons and is very confusing.
I, moreover, believe that "400 Bad Request", "404 Not Found", or - rather - "501 Not Implemented" would have been rather more appropriate HTTP return states.
I created a GitHub issue on this:
"405: Method not allowed" = Misleading error message → Replace with better HTTP status code
How to get PUT and DELETE verbs to work with WebAPI on IIS
Actually removing WebDAV from my server caused the application to return a 503 Service Unavailable
Error message, so I re-installed it.
I was able to fix this by disabling WebDAV for the individual application pool, this stopped the 'bad module' error.
Disable WebDAV for Individual App Pool:
- Click the affected web site in IIS
- Find
WebDAV Authoring Tools
in the list - Click to open it
- Click
Disable WebDAV
in the top right.
Ta daaaa!
This link is where I found the instructions but it's not very clear.
Related Topics
How to Return a String Repeated X Number of Times
.Net Console Application as Windows Service
How to Bind an Enum to a Combobox Control in Wpf
Correct Use of Multimapping in Dapper
Microsoft.Jet.Oledb.4.0' Provider Is Not Registered on the Local MAChine
What Is Hashcode Used For? Is It Unique
Iterate Two Lists or Arrays with One Foreach Statement in C#
Getting a Url with an Url-Encoded Slash
Blazor - Display Wait or Spinner on API Call
Regular Expression Groups in C#
A Field Initializer Cannot Reference the Non-Static Field, Method, or Property
Executenonquery: Connection Property Has Not Been Initialized
How to Dynamically Create ASP.NET Controls Within Dynamically Created ASP.NET Controls
Arrays, Heap and Stack and Value Types
C# If/Then Directives for Debug VS Release
Programmatically Determine a Duration of a Locked Workstation