简体   繁体   中英

Calling DELETE method in Web API

In a new Web API project with :

  • GET method

     // GET api/values/5 public string Get(int id) { return "value"; } 
  • DELETE method

     // DELETE api/values/5 public void Delete(int id) { var client = new MongoClient("mongodb://localhost:27017"); var server = client.GetServer(); var db = server.GetDatabase("Test"); var collection = db.GetCollection<Entity>("Entities"); var deleteEntity = Query<Entity>.EQ(e => e.Id, id); collection.Remove(deleteEntity); } 

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?

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.

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

Use...

Get: /controller/123456

Delete: /controller/id/123456

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM