简体   繁体   English

在Web API中调用DELETE方法

[英]Calling DELETE method in Web API

In a new Web API project with : 在一个新的Web API项目中:

  • GET method GET方法

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

     // 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 . 他们有一个类似的URL: api/values/5

When I want to call the Delete method, it executes the Get method. 当我想调用Delete方法时,它会执行Get方法。 What do I do? 我该怎么办?

The URL is the same but you invoke this URL programaticaly with a "DELETE" 'http method' rather than "GET". URL是相同的,但您使用“DELETE”'http方法'而不是“GET”来调用此URL。 If you are just navigating to the URL in your browser, the browser will only do a GET. 如果您只是浏览浏览器中的URL,浏览器将只执行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. 如何以编程方式执行DELETE(或POST或PUT)将取决于您用于调用服务的库,但它们都倾向于使用某种参数或属性来设置此方法。

Take a look at the HttpDelete attribute: 看一下HttpDelete属性:

https://msdn.microsoft.com/en-us/library/system.web.mvc.httpdeleteattribute(v=vs.118).aspx 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: 你需要像这样装饰你的方法,以便MVC知道如何处理传入的请求:

[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: 如果您通过HTML表单提交删除方法,请记住它们仅支持POST和GET方法,因此您需要通过JavaScript提交DELETE:

http delete request from browser 来自浏览器的http删除请求

Use... 使用...

Get: /controller/123456 获取:/ controller / 123456

Delete: /controller/id/123456 删除:/ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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