实体部分中的更新的WebAPI PUT / POST [英] Partial Entity Updates in WebAPI PUT/POST

查看:139
本文介绍了实体部分中的更新的WebAPI PUT / POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个存储库的方法来更新文件:

Say you have a repository method to update a Document:

public Document UpdateDocument(Document document)
  {
  Document serverDocument = _db.Documents.Find(document.Id);
  serverDocument.Title = document.Title;
  serverDocument.Content = document.Content;
  _db.SaveChanges();
  return serverDocument;
  }

在这种情况下,该实体有两个属性。当更新文档,这两个属性都需要在JSON请求,所以 PUT / API /文件夹的请求与身体

In this case, the entity has two properties. When updating a Document, both of these properties are required in the JSON request, so a request to PUT /api/folder with a body of

{
  "documentId" = "1",
  "title" = "Updated Title"
}

因为内容未提供将返回一个错误。我这样做的原因是,即使为空的特性和属性,用户不更新,它似乎更安全强制客户端中,以避免覆盖与服务器端的空场不详请求指定这些字段。

would return an error because "content" was not provided. The reason I'm doing this is because, even for nullable properties and properties that the user doesn't update, it seems safer to force the client to specify these fields in the request to avoid overwriting unspecified fields with nulls serverside.

这导致我总是需要在PUT和POST请求的每个更新的财产,即使这意味着这些属性指定空的做法。

这是很酷的,或者是有,我还没有了解还可能方便,只需要在哪些线路发送部分更新模式/实践?

Is this cool, or is there a pattern/practice that I haven't learned about yet that might facilitate partial updates by sending only what is needed over the wire?

推荐答案

在API设计的最佳实践是使用HTTP补丁部分更新。
事实上,使用像你这样的情况下是为什么IETF首先介绍了它的真正原因。

The best practice in API design is to use HTTP PATCH for partial updates. In fact, use cases like yours are the very reason why IETF introduced it in the first place.

RFC 5789定义非常precisely:

RFC 5789 defines it very precisely:

PATCH 用于应用的部分修改以一个资源。

PATCH is used to apply partial modifications to a resource.

一个新的方法是必要的,以提高互操作性和prevent结果
  错误。在 PUT 方式已经被定义来覆盖资源的结果
  一个完整的新的身体,和不能再用做局部修改即可。
  否则,代理和缓存,甚至客户端和服务器,可能会得到结果
  混淆的操作的结果。 POST已被使用,但
  没有广泛的互操作性(为一体,是没有标准的方法来搜索
  发现补丁格式支持)。

A new method is necessary to improve interoperability and prevent
errors. The PUT method is already defined to overwrite a resource
with a complete new body, and cannot be reused to do partial changes. Otherwise, proxies and caches, and even clients and servers, may get
confused as to the result of the operation. POST is already used but without broad interoperability (for one, there is no standard way to
discover patch format support).

马克诺丁汉写了伟大的文章有关API设计中使用补丁 - 的 http://www.mnot.net/blog/2012/09/05/patch

Mark Nottingham has written a great article about the use of PATCH in API design - http://www.mnot.net/blog/2012/09/05/patch

在你的情况,这将是:

  [AcceptVerbs("PATCH")]
  public Document PatchDocument(Document document)
  {
      Document serverDocument = _db.Documents.Find(document.Id);
      serverDocument.Title = document.Title;
      serverDocument.Content = document.Content;
      _db.SaveChanges();
      return serverDocument;
  }

这篇关于实体部分中的更新的WebAPI PUT / POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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