为什么需要 PUT 或 DELETE Http 动词?

发布于 2024-08-26 06:49:12 字数 253 浏览 8 评论 0原文

MVC 2 发布后,我开始检查和使用新功能,但我无法理解 PUTDELETE 动词有什么用。

我搜索过它并阅读了一些文章,但我无法理解。

DELETEPUT 的主要用途是什么?与使用 GETPOST 方法相比,它们是否有任何优势(即使我可以使用 GET 和 POST 处理所有请求)?

After the release of MVC 2, I have started to check and play with the new features, but i couldn't understand what is the use of PUT and DELETE verbs.

I have searched about it and read some articles but I couldn't get it.

What is the main purpose of DELETE and PUT? Do they have any advantages over using a GET or POST method instead (even though I can handle all of the requests with GET and POST)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滴情不沾 2024-09-02 06:49:12
  • GET:唯一的功能是将信息发送回客户端。它应该是一个可重复的操作,没有副作用。

  • POST:它执行有副作用的操作。它是不可重复(如果您POST两次,服务器会执行两次) 。操作后,它应该重定向到另一个页面以使用 GET 显示结果。

  • DELETE:它的唯一功能是进行破坏性操作,不可重复(一旦删除对象,就没有其他可删除的内容)。

  • PUT:它的功能是修改单个对象并使用以 POST(类似)方式发送的值来更新它。可重复。

您可以使用 POST 来伪造 DELETE 和 PUT(因为某些网络浏览器无法识别 DELETE 和 PUT)。

请仅使用 GET 来显示信息,不适用于有副作用的操作

  • GET: Only function is to send information back to the client. It should be a repeatable operation without side effects.

  • POST: It does operations with side effects. It is not repeatable (if you POST twice, the server acts twice). After operation it should redirect to another page to show the results using GET.

  • DELETE: Its only function is to do a destructive operation, not repeatable (once the object is deleted, there is nothing else to delete).

  • PUT: Its function is to modify a single object and update it with the values sent in a POST (like) way. Repeatable.

You can fake DELETE and PUT with POST (as some web browsers don't recognize DELETE and PUT).

Please, use GET only to display information, not for operations with side effects.

节枝 2024-09-02 06:49:12

在 RESTful 架构中,DELETE 应该用于删除数据的请求,PUT 应该用于插入数据的请求。

In a RESTful architecture, DELETE is supposed to be used for requests that will remove data, and PUT is supposed to be used for requests that will insert data.

¢好甜 2024-09-02 06:49:12

基本上它用于更好地区分操作/权限。

幂等方法和 Web 应用程序

方法 PUT 和 DELETE 定义为
是幂等的,这意味着多个
相同的请求应该有
与单个请求的效果相同。
方法 GET、HEAD、OPTIONS 和 TRACE,
被规定为安全的,也应该
是幂等的,因为 HTTP 是无状态的
协议。相比之下,POST 方法
不一定是幂等的,并且
因此发送相同的 POST
多次请求可能会进一步
影响状态或造成进一步的影响
影响(例如财务
交易)。在某些情况下,这可能
是可取的,但在其他情况下,这
可能是由于意外事故,例如
当用户没有意识到时
他们的行动将导致发送
另一个请求,或者他们没有
收到足够的反馈
第一次请求成功。尽管
网络浏览器可能会显示警报对话框
在某些情况下警告用户的框
重新加载页面可能会重新提交
POST请求,一般可达
处理案件的网络应用程序
不应使用 POST 请求的地方
提交多次。注意
方法是否幂等不是
由协议或网络强制执行
服务器。完全有可能
编写一个 Web 应用程序,其中(对于
例如)数据库插入或其他
非幂等动作由以下条件触发
GET 或其他请求。忽略这一点
然而,建议可能会导致
如果用户
代理假设重复相同的操作
当请求不安全时,请求就是安全的。

通过维基百科
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

Basically it's used to better distinguish actions/privileges.

Idempotent methods and web applications

Methods PUT and DELETE are defined to
be idempotent, meaning that multiple
identical requests should have the
same effect as a single request.
Methods GET, HEAD, OPTIONS and TRACE,
being prescribed as safe, should also
be idempotent, as HTTP is a stateless
protocol. By contrast, the POST method
is not necessarily idempotent, and
therefore sending an identical POST
request multiple times may further
affect state or cause further side
effects (such as financial
transactions). In some cases this may
be desirable, but in other cases this
could be due to an accident, such as
when a user does not realize that
their action will result in sending
another request, or they did not
receive adequate feedback that their
first request was successful. While
web browsers may show alert dialog
boxes to warn users in some cases
where reloading a page may re-submit a
POST request, it is generally up to
the web application to handle cases
where a POST request should not be
submitted more than once. Note that
whether a method is idempotent is not
enforced by the protocol or web
server. It is perfectly possible to
write a web application in which (for
example) a database insert or other
non-idempotent action is triggered by
a GET or other request. Ignoring this
recommendation, however, may result in
undesirable consequences if a user
agent assumes that repeating the same
request is safe when it isn't.

via wikipedia
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

绅士风度i 2024-09-02 06:49:12

首先,您应该查看 BlaM 的非常好的答案对于这个(欺骗?)问题。

显然,从技术上讲,您可以在不使用 REST 原则的情况下创建/更新/删除资源,但您忽略了一点。如果您仍然没有真正了解 REST 背后的概念,Ryan Tomayko 的博客文章 是一个不错的起点。

First, you should check out BlaM's very good answer to this (dupe?) question.

Obviously you can technically create/update/delete resources without using REST principles, ut you're missing a point. If you still don't really get the concepts behind REST, Ryan Tomayko's blog entry is a nice place to start.

阳光下的泡沫是彩色的 2024-09-02 06:49:12

最初的目的是使用这些动词编辑网页(有关 RESTful 系统的更多信息)。此后,它们已被 WebDAV 扩展弃用。实际上,PUT 和 DELETE 从未被使用过(或者很少被定制的应用程序使用)。

The original purpose was to edit webpages using those verbs (more on the RESTful system). They have since been deprecated by the WebDAV extension. In practice, PUT and DELETE are never used (or very rarely by custom built applications).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文