为什么需要 PUT 或 DELETE Http 动词?
MVC 2 发布后,我开始检查和使用新功能,但我无法理解 PUT
和 DELETE
动词有什么用。
我搜索过它并阅读了一些文章,但我无法理解。
DELETE
和 PUT
的主要用途是什么?与使用 GET
或 POST
方法相比,它们是否有任何优势(即使我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
在 RESTful 架构中,
DELETE
应该用于删除数据的请求,PUT
应该用于插入数据的请求。In a RESTful architecture,
DELETE
is supposed to be used for requests that will remove data, andPUT
is supposed to be used for requests that will insert data.基本上它用于更好地区分操作/权限。
幂等方法和 Web 应用程序
通过维基百科
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
Basically it's used to better distinguish actions/privileges.
Idempotent methods and web applications
via wikipedia
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
首先,您应该查看 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.
最初的目的是使用这些动词编辑网页(有关 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).