Tanvir
Tanvir

Reputation: 95

Calling DELETE method in Web API

In a new Web API project with :

They have a similar URL: api/values/5.

When I want to call the Delete method, it executes the Get method. What do I do?

Upvotes: 4

Views: 32482

Answers (4)

nabukhas
nabukhas

Reputation: 193

The config piece below solved my problem:

<validation validateIntegratedModeConfiguration="false" />
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />

  <!--This will enable all Web API verbose-->
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

Upvotes: 0

Rob Bell
Rob Bell

Reputation: 3665

Take a look at the HttpDelete attribute:

https://msdn.microsoft.com/en-us/library/system.web.mvc.httpdeleteattribute(v=vs.118).aspx

You need to decorate your methods like this so that MVC knows how to handle the incoming request:

[HttpGet]
public string Get(int id)
{
    ...
}

[HttpDelete]
public void Delete(int id)
{
    ...
}

If you're submitting to the delete method via an HTML form, bear in mind they only support the POST and GET methods, so you'll need to submit the DELETE via JavaScript:

http delete request from browser

Upvotes: 3

lem2802
lem2802

Reputation: 1162

Use...

Get: /controller/123456

Delete: /controller/id/123456

Upvotes: 0

Robert Levy
Robert Levy

Reputation: 29073

The URL is the same but you invoke this URL programaticaly with a "DELETE" 'http method' rather than "GET". If you are just navigating to the URL in your browser, the browser will only do a GET. How you programmatically do a DELETE (or POST or PUT) will depend on what library you are using to invoke the service but they all tend to have some kind of parameter or property called 'method' for setting this.

Upvotes: 6

Related Questions