【问题标题】:Implementing PATCH in webapi在 webapi 中实现 PATCH
【发布时间】:2014-01-25 17:52:38
【问题描述】:

我有一个对象,我想部分地使用 webapi/json 更新这里是我的模型的一个例子

   public class Location
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
    }

来自客户端的 JSON 将是

 {
    "Id": 1, 
    "Address":"new address" 
  }

webapi函数长这样

    public bool Patch(Location location)
    {
        //do something
    }

问题是唯一更新的字段是地址,因此如果不检查每个字段是否有 string.isnullorempty,我无法判断发生了什么变化,而更多的 null/empty 可能只是意味着删除该值是否有更无缝的方法来做到这一点?

【问题讨论】:

标签: asp.net json http asp.net-web-api


【解决方案1】:

ASP.NET Web API 本身不支持 JSON Patch。目前有两种可用于 .NET 的 JSON 补丁规范实现(至少我知道):

myquay/JsonPatch
Github:https://github.com/myquay/JsonPatch
NuGet:https://www.nuget.org/packages/JsonPatch/1.0.0

KevinDockx/JsonPatch
GitHub:https://github.com/KevinDockx/JsonPatch
NuGet:https://www.nuget.org/packages/Marvin.JsonPatch/0.3.0

这两个目前都处于“alpha”状态,并且都没有完全实现规范。

【讨论】:

【解决方案2】:

不是真的。这就是为什么有Json-patch 然而,据我所知,没有人为此编写过.net 库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多