为什么在WEBAPI中使用HTTPPOST时可以使用HTTPPUT [英] Why to use HTTPPUT when same can be done with HTTPPOST in WEBAPI

查看:83
本文介绍了为什么在WEBAPI中使用HTTPPOST时可以使用HTTPPUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说HTTPPUT请求更新特定URI的对象,但是我们可以通过HTTPPOST请求(WEBAPI .Net)实现何时使用HTTPPOST和HTTPPUT。



我尝试过:



我在APIController继承的类中创建了HTTPPOST和HTTPPUT方法。并使用相同的方法更新

Httppost Method.I不知道为什么我们需要HTTPPUT当HTTPPOST存在时,或者只是提到我们已经更新了已经存在的东西。

It is said that HTTPPUT Request updates the Object at Specific URI, but same thing we can achieve by HTTPPOST Request( WEBAPI .Net ) when to use HTTPPOST and When HTTPPUT.

What I have tried:

I created both HTTPPOST and HTTPPUT method in my APIController inherited class .And update with the same
Httppost Method.I am not getting why we need HTTPPUT when HTTPPOST is there or it is just to mention that we are updatng something that already exist.

推荐答案

PUT是幂等的,所以如果你将一个对象PUT两次,它就没有效果。这是一个不错的属性,所以我会尽可能使用PUT。



这两种方法都可以使用,但你需要证明这一点。



您可以参考以下帖子;



http-PUT与POST中的POST - 堆栈溢出 [ ^ ]
PUT is idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

Both method could be used, but you need to justify.

You could refer following thread;

http - PUT vs POST in REST - Stack Overflow[^]


在我的这篇文章中, ASP.NET 5 Web API RESTful CRUD和Windows 10本机应用程序 [ ^ ],我已经谈过了关于HTTP动词,POST和PUT。问题不在于使用哪一个,问题是如何以一种他们想要使用的方式使用HTTP动词。



使用不良做法,我可以使用GET方法HTTP请求上传内容,更新内容甚至创建新记录。我可以传递要保存为查询字符串的数据,然后保存。当你有一天要公开发布API时,这不是使用HTTP动词的好方法。这就是为什么,在做某事之前,你应该总是去阅读实际的文件。



无论如何,HTTP动词用作:



1. HTTP GET:用于请求获取该URI的资源。

2. HTTP DELETE:用于请求删除该URI处的资源。

3. HTTP POST:用于请求上传并保存正在上传的数据到数据。然后,服务器存储实体并为该资源提供新的URI。

4. HTTP PUT:与POST相同,但条件是它检查是否已保存该资源。如果该资源可用,那么它只是更新。



这些是每个人应该知道的HTTP谓词的差异,应该使用HTTP作为协议编写他们的Web API,因此。



有关详情,请参阅以下文件。



超文本传输​​协议 - 维基百科,免费的百科全书 [ ^ ]

HTTP方法 [ ^ ]

< a href =https://msdn.microsoft.com/en-us/library/system.web.mvc.httpverbs(v=vs.118).aspx> HttpVerbs Enumeration (System.Web.Mvc) [ ^ ](仅供参考)
In this article of mine, ASP.NET 5 Web API RESTful CRUDs and Windows 10 native application[^], I have talked about the HTTP verbs, POST and PUT. The problem is not with using which one, the problem is how to use the HTTP verbs in a way that they are meant to be used.

Using bad practices, I can upload something, update something or even create a new record all using the GET method HTTP request. I can pass the data which is to be saved as a query string and there, it is saved. That is not the good way of using the HTTP verbs when you are going to publish the API someday for public. That is why, you should always go and read the actual documentations before doing something.

Anyways, the HTTP verbs are used as:

1. HTTP GET: For requesting to get the resource at that URI.
2. HTTP DELETE: For requesting to delete the resource at that URI.
3. HTTP POST: For requesting to upload and save the data being uploaded to the data. Server then stores the entity and provides a new URI for that resource.
4. HTTP PUT: Same as POST but with a condition that it checks if that resources is already saved. If that resource is available then it simply updates.

These are the differences in the HTTP verbs that everyone should know and should write their web APIs using HTTP as protocol, accordingly.

For more on this, please refer to the following documents.

Hypertext Transfer Protocol - Wikipedia, the free encyclopedia[^]
HTTP Methods[^]
HttpVerbs Enumeration (System.Web.Mvc)[^] (For understanding only)


这篇关于为什么在WEBAPI中使用HTTPPOST时可以使用HTTPPUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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