asp.net-core – 在JsonPatchDocument中使用.Net Core Web API

asp.net-core – 在JsonPatchDocument中使用.Net Core Web API,第1张

概述我正在使用JsonPatchDocument来更新我的实体,如果 JSON如下所示,这很有效 [ { "op": "replace", "path": "/leadStatus", "value": "2" },] 当我创建对象时,它将使用Operations节点对其进行转换 var patchDoc = new JsonPatchDocument<LeadTransDetail>();p 我正在使用JsonPatchdocument来更新我的实体,如果 JSON如下所示,这很有效

[  { "op": "replace","path": "/leadStatus","value": "2" },]

当我创建对象时,它将使用Operations节点对其进行转换

var patchDoc = new JsonPatchdocument<LeadTransDetail>();patchDoc.Replace("leadStatus",statusID); {  "Operations": [    {      "value": 2,"op": "replace","from": "string"    }  ]}

如果JsON对象看起来像Patch不起作用.我相信我需要使用它来转换它

public static voID ConfigureAPIs(httpConfiguration config){    config.Formatters.Add(new JsonPatchFormatter());}

这应该解决,问题是我使用.net核心所以不是100%肯定在哪里添加JsonPatchFormatter

解决方法 我使用ASP.NET Core 1.0版创建了以下示例控制器.如果我发送您的JsON-Patch-Request

[  { "op": "replace",]

然后在调用ApplyTo后,属性leadStatus将被更改.无需配置JsonPatchFormatter. Ben Foster写的一篇好文章帮助我获得了更多的理解 – http://benfoster.io/blog/aspnet-core-json-patch-partial-api-updates

public class PatchController : Controller{    [httpPatch]    public IActionResult Patch([FromBody] JsonPatchdocument<LeadTransDetail> patchdocument)    {        if (!ModelState.IsValID)        {            return new BadRequestObjectResult(ModelState);        }        var leadTransDetail = new LeadTransDetail        {            LeadStatus = 5        };        patchdocument.ApplyTo(leadTransDetail,ModelState);        if (!ModelState.IsValID)        {            return new BadRequestObjectResult(ModelState);        }        return Ok(leadTransDetail);    }}public class LeadTransDetail{    public int LeadStatus { get; set; }}

希望这可以帮助.

总结

以上是内存溢出为你收集整理的asp.net-core – 在JsonPatchDocument中使用.Net Core Web API全部内容,希望文章能够帮你解决asp.net-core – 在JsonPatchDocument中使用.Net Core Web API所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1239557.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存