插入和更新的 URI 约定 - WCF Rest API
我有用户集合和用户集合。用户 - ID、FName、LName。
对于插入,我传递带有 Id、FName、LName 的 User 实例。如果用户不存在,服务将插入该用户。
为了更新,我再次传递带有所有参数的 User 实例。该服务更新具有给定 ID 的用户的 FName 和 LName。
我想在服务中将插入和更新作为两个单独的方法。
我不能为这两种方法提供相同的 URI。如下所示:
[WebInvoke(UriTemplate = "Users", Method = "PUT")]
void UpdateUser(User instance);
[WebInvoke(UriTemplate = "Users", Method = "PUT")]
void AddUser(User instance);
实现此目标的最佳方法是什么?
我不同意一篇文章说更新 URI 以具有以下内容:
[WebInvoke(UriTemplate = "Users/{userId}", Method = "PUT")]
void UpdateUser(string userId, User instance);
因为,用户 id 已经存在于 Userinstance 中。
虽然我同意 PUT 方法可以执行插入和更新。出于某种原因,我需要将它们分开。
I have Users collection with User's. User - Id, FName, LName.
For insert, I pass User instance with Id, FName, LName. The service inserts the User if it's not present.
For update, I again pass User instance with all parameters. The service updates FName and LName for the User with the given Id.
I want to have insert and update as 2 separate methods in the service.
I can't have same URI's for both methods. Something like below:
[WebInvoke(UriTemplate = "Users", Method = "PUT")]
void UpdateUser(User instance);
[WebInvoke(UriTemplate = "Users", Method = "PUT")]
void AddUser(User instance);
Which is the best way to acheive this?
I don't agree with one post saying update URI to have something like :
[WebInvoke(UriTemplate = "Users/{userId}", Method = "PUT")]
void UpdateUser(string userId, User instance);
Because, user id is already present in the Userinstance.
Though I agree that PUT method can perform insert and update. For some reason I need to keep them separate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PUT,作为一个 幂等 操作,应该在您要 PUT 到的 URL 处创建一个新资源,或者完全创建一个新资源替换现有资源(在此放置此资源)。如果对同一个 URL 多次执行相同的 PUT,您仍然会得到相同的资源。
POST 不一定是幂等的,因为您正在更改现有资源。如果您的 POST 正在执行类似向订单添加商品之类的操作,并且您将相同的数据多次发布到相同的 url,则最终可能会得到包含多个相同商品的订单。
简短的回答,让您插入 PUT 操作并更新 POST。
我认为这个答案很好地解释了它。
PUT, being an idempotent operation should create a new resource at the URL you are PUTting to or completely replace the existing resource (PUT this resource here). If you perform the same PUT to the same URL multiple times you will still end up with the same resource.
POST is not necessarily idempotent, because you are changing an existing resource. If your POST is doing something like adding items to an order and you POST the same data to the same url mutliple times you could end up with an order containing multiples of the same item.
Short answer, make your insert a PUT operation and the update a POST.
I think this SO Answer does a really good job of explaining it.
您可以通过几种方式查看它
创建、插入任意次数
即使您传递相同的数据也喜欢
多次,因为它忽略了 ids
所以我会说:
获取用户信息一样,使用 GET ... 获取用户信息 /Users/{userId} ... 或使用 Delete 删除用户 /Users/{userId} 。
不确定这是否有帮助,如果没有,请告诉我,我会再试一次:-)
You can look at it a couple of ways
creates, insert as many times as it
likes even if you pass the same data
in multiple times as it ignores the ids
So I'd say:
As it would then be to get the user info, /Users/{userId} with GET ... or to delete the user, /Users/{userId} with Delete.
Not sure if this helps or not, if not then let me know and I'll try again :-)