DELETE方法不工作 [英] DELETE method not working

查看:162
本文介绍了DELETE方法不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了包含删除客户端具有下列方法的WebAPI控制器...

I have written a WebApi controller that contains the following method for deleting a client...

[HttpDelete]
public void DeleteClient(int id) {
  // do stuff here
}

......,我试图通过使用网页...

...and am trying to test it by using the following HTML on a web page...

<form method="DELETE" action="/api/ClientsXml/">
  <table style="padding: 5px">
    <tr>
      <td>ID</td>
      <td><input type="text" size="20" name="id" id="id" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" value="Send" /></td>
    </tr>
  </table>
</form>

不过,DeleteClient方法不会被调用。它贯穿于GetClient方法,而不是请求。

However, the DeleteClient method is never called. It passes the request through to the GetClient method instead.

任何人任何想法,为什么?我已经试过各种变化,但我就是不能让所谓的删除方法。

Anyone any idea why? I've tried all sorts of variations, but I just can't get the delete method called.

推荐答案

@ DimitryS的答案是正确的,但我想我会建立一点就可以了。

@DimitryS's answer is correct, but I thought I'd build on it a little.

HTML表单只允许GET和POST操作。这是 HTML 5规范当前以及在HTML&LT; 4规格。

HTML forms only allow GET and POST operations. This is current in the HTML 5 spec as well as the HTML < 4 spec.

其他的HTTP方法使用的 XMLHtt prequest 这就是underlies jQuery的Ajax功能。所以一个好的选择是使用jQuery你放,删除和它应该在所有的主流浏览器的工作(在此pretty明确的SO问题的一些讨论:<一href=\"http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers\">Are在大多数Web浏览器提供的PUT,DELETE,HEAD等方法呢?)。

Other HTTP methods are allowed when using XMLHttpRequest which is what underlies jQuery's ajax functionality. So a good option could be to use jQuery for your PUTs, DELETEs, and it should work in all major browsers (some discussion of that in this pretty definitive SO question: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?).

最后,我会说,如果你只是使用的形式来测试您的API,那么为什么不尝试一个浏览器扩展,而不是:
例如

Lastly, I'll say that if you are just using the form to test your API, then why not try a browser extension instead: e.g.

  • POSTMan for chrome
  • RESTClient for firefox

有更多的人,但多数让您保存一个测试套件,设置不同的页眉,等等。

There are many more, but most allow you to save a test suite, set different headers, and so forth.

这篇关于DELETE方法不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆