加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在webapi中实现PATCH

发布时间:2020-12-16 09:53:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个对象,我想使用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; } } 来自客户端
我有一个对象,我想使用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的每个字段我不知道什么改变了更多的空/空可能只是意味着删除值是否有更无缝的方式来做到这一点?

解决方法

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

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”状态,并且它们都没有完全实现规范.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读