13

I already worked with web API and had a lot of problems like posting multiple parameters. I upgraded to WebApi2 to use routing attributes and now have problems like:

"message":"The requested resource does not support http method 'DELETE'."

I spent all day searching Stack Overflow and the web to solve the problem:

  • Removed webdav
  • In http protocol allow all get,put,post,delete
  • Added the [HTTPDelete] attribute
  • Added name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"
  • Searched Google for the necessary help here

Can someone please guide me on this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2997115
  • 159
  • 1
  • 1
  • 7

3 Answers3

25

I had the same problem. Adding the below code to your web.config should fix the problem under the system.webserver section:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="WebDAVModule"/>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vinoth
  • 2,419
  • 2
  • 19
  • 34
11

I had the same problem, because my controller was like this:

[HttpDelete]
public HttpResponseMessage Delete(string Id)
{
     ....
}

And on the client I used the send the ID as request body:

var url = '/api/upload';
var data = JSON.stringify({ 'Id': id }); // <-- In the body
$.ajax({
    url: url,
    type: 'DELETE',
    data: data, // <-- in the body
    contentType: 'application/json'
})

When I changed the client to use it as URL parameter instead it worked:

var url = '/api/upload/' + id; // <- In the URL
$.ajax({
    url: url,
    type: 'DELETE',
    contentType: 'application/json'
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fábio Correia
  • 962
  • 7
  • 8
4

The JavaScript code for the DELETE verb must be like this:

$.ajax({
    **url: "/api/SomeController/" + id,**
    type: "DELETE",
    dataType: "json",
    success: function(data, statusText) {
        alert(data);
    },
    error: function(request, textStatus, error) {
        alert(error);
        debugger;
    }
});

Do NOT use something like this,

...
data: {id:id}
...

as when you are using the POST method.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pavel Kharibin
  • 765
  • 2
  • 15
  • 24