90

I use HTTP PUT and DELETE in my ASP.NET MVC3 application. When I run it in local, every thing works correctly; But when I publish the application to the server, these methods do not work.

Are there any special settings for enable a web server to support PUT and DELETE requests? I'm using shared hosting with IIS 7.5.

I enable PUT and DELETE requests in IIS manager. PUT command work fine. But DELETE still not works. I create requests by jQuery:

I'm in this page:

http://example.com/dashboard/edit-site/103323/links/

and my AJAX call is:

$.ajax({
    // url: same as page-url,
    cache: false,
    type: 'DELETE',
    data: { linkid: $(link).data("linkid") },
    beforeSend: function () {
        // doing something in UI
    },
    complete: function () {
        // doing something in UI
    },
    success: function (data) {
        // doing something in UI
    },
    error: function () {
        // doing something in UI
    }
});

This will create a request like this:

Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Origin: http://example.com
Referer: http://example.com/dashboard/edit-site/103323/links/
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
X-Requested-With: XMLHttpRequest

With this Form Data:

linkid:104044
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • 1
    I wonder if there's a way to split this question up into two pieces - The first half regarding enabling PUT on IIS was very helpful to me, the second, about correct Jquery code, was not... Glad you (and I) got the answer, just wondering if we can possibly improve this question's structure. – Bryan Rayner Jun 24 '15 at 20:58
  • Readers - Also see Microsoft: [Troubleshooting HTTP 405 errors](https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications) – Yogi Apr 29 '19 at 09:56

6 Answers6

119

Go to Handler Mappings in your IIS Manager. Find ExtensionlessUrlHandler-Integrated-4.0, double click it. Click Request Restrictions... button and on Verbs tab, add both DELETE and PUT.


Possible WebDav Publisher issue

You've mention on a deleted post you were running on a 2008 server right? Try removing webDav role, or disable it from your site config: on system.webServer -> modules section, remove WebDAVModule module:

<system.webServer>
  <modules>
    <remove name="WebDAVModule" />
  </modules>
  <handlers>
    <remove name="WebDAV" />
  </handlers>
</system.webServer>
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
danielQ
  • 2,016
  • 1
  • 15
  • 19
  • 9
    Let's try removing WebDav. See answer Edit: **Possible WebDav Publisher issue**. – danielQ Sep 17 '12 at 16:39
  • +Daniel I did. Still not works. I see just now, when I make a DELETE request, and while it's trying to do request I click another link, and the DELETE request sone fine! My english is too bad, do you understande what is my mean? See: 1- Make a DELETE request, 2- It's trying, 3- I click another link, 4- The DELETE request (that was trying) get succeed! – amiry jd Sep 17 '12 at 18:55
  • I forgot to note. I make requests via jQuery – amiry jd Sep 17 '12 at 18:55
  • +Daniel Thanks for your suggestions. I'll +1 your answer to say thank you. The app is a big app and many times I used DELETE and PUT and it's really dificault to change all of them. For example in page `/mycontroller/id/action/` I have for action-methods: GET, POST, PUT, DELETE and if I want to change http-command usages, I'll be forced to create actually a new routing system. Thanks again. – amiry jd Sep 17 '12 at 19:51
  • Removing WebDAV was the solution for me! Thanks for the edit! – Vinney Kelly Jul 11 '16 at 04:04
  • Works like charm! – Kumee Jan 19 '17 at 09:26
  • 4
    It's important to remove both the module and the handler. I missed removing the module the first time in IIS and was very confused for awhile! – jocull Jan 25 '18 at 13:27
  • this finally worked for me, adding the sections to the web.config is what finally got my project working (asp.net core & MVC) – d0rf47 Aug 10 '22 at 16:30
65

If you are getting following error in your production environment in the asp.net web api on PUT or DELETE though these methods are working fine locally.

405 - http verb used to access this page is not allowed.

Just add following settings in your server's web.config

<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>

Cause: webDAV module blocks PUT/DELETE methods by default. So first remove this module and its handler. We first remove any existing ExtensionlessUrlHandler-Integrated-4.0 settings and add it with desired path and verbs.

Liam
  • 27,717
  • 28
  • 128
  • 190
Heera Jaiswal
  • 681
  • 5
  • 3
  • I missed the module and only removed the handler and it didn't work, then came back and saw you removed the module as well and when I did that it worked for me as well. Just a note to anyone finding this via google that removing just the handler is not enough. – ldam May 04 '18 at 13:49
  • Worked like a charm, Used it when web-api2 was failing for PUT/ DELETE verbs... You saved me – Himalaya Garg Oct 24 '18 at 17:46
  • Still not working here. Even remove the module. But in my case, I'm trying to use this on a simple console application. – Darós Jan 07 '19 at 13:42
  • not working on my side. Heading out it since one day but not found any solution. Try all solutions but not any helpful. – Zeeshan Safdar Oct 09 '19 at 05:43
  • 1
    Can someone explain a little more what is going on to make this work? It seems to be removing a lot and adding a specific version (4.0) which I might assume has changed over time. – ModestMonk Aug 26 '20 at 19:10
18

You just need to add the following lines of code in your web.config

<system.webServer>
 <security>
    <requestFiltering>
        <verbs allowUnlisted="false">
            <add verb="GET" allowed="true" />
            <add verb="POST" allowed="true" />
            <add verb="DELETE" allowed="true" />
            <add verb="PUT" allowed="true" />
        </verbs>
    </requestFiltering>
</security>

AND

<modules>
    <remove name="WebDAVModule" />
</modules>
<handlers>
    <remove name="WebDAV" />
</handlers>
Adeel Asghar
  • 217
  • 2
  • 5
  • Not working for me. I hosted wordpress on mysite.com/wordpress/index.php/wp-json/wp/v2/posts/649?force=true. I try to delete but I get 405 method not allowed. – Vlado Pandžić Oct 10 '17 at 12:38
  • 1
    Is this version specific? It seems like I see this same answer all over the web, but in IIS 8.0 it just causes a 500 internal server error. Maybe we need a new solution. – Typel Oct 11 '17 at 18:53
  • @Typel - see [PUT & DELETE Verbs Not Allowed - IIS 8](https://stackoverflow.com/a/10907343/943435) – Yogi Apr 29 '19 at 08:39
  • Worked for me when using Azure App Service and Docker w Linux. Thanks! – Jonas Lincoln Jun 16 '21 at 10:15
0

Finally I find the answer fluky. I changed the jQuery call to tho below and it's working well now.

$.ajax({ 
    url: this.href + "?linkid=" + $(link).data("linkid"), 
    cache: false, 
    type: 'DELETE', 
    // data: { linkid: $(link).data("linkid") }, 
    beforeSend: function () { 
        // doing something in UI 
    }, 
    complete: function () { 
        // doing something in UI 
    }, 
    success: function (data) { 
        // doing something in UI 
    }, 
    error: function () { 
        // doing something in UI 
    } 
});

Do you have any explanation why a DELETE call, can't have Form Data? While on local it had and worked fine?

P P P
  • 227
  • 3
  • 16
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • 4
    A delete request by definition does not have any posted parameters. – Naftali Nov 08 '12 at 18:13
  • @Neal yep you are right. Post your comment as an answer that I can accept it – amiry jd Nov 28 '12 at 08:16
  • 3
    @Neal - There is nothing in the HTTP 1.1 spec that forbids or even suggests that a `DELETE` request should not have a message body. Can you provide a link indicating otherwise? – Anthony Jul 08 '15 at 05:35
0

I have met the same issue. You guys only access on server deploy API and uninstall WebDAV and it done. You can reference this

https://achrafbenalaya.com/2020/10/17/405-method-not-allowed-in-iis/

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 04 '21 at 16:36
0

The proper way to do it is to figure out the real blocker and then remove this source instead of guessing around. Please check this how here: https://stackoverflow.com/a/70530342/592651

Ashi
  • 806
  • 1
  • 11
  • 22