【问题标题】:JSON Patch 'replace' entire list of stringsJSON补丁“替换”整个字符串列表
【发布时间】:2021-02-08 02:20:41
【问题描述】:

我在使用 JSON Patch 执行更新时遇到问题。在这种情况下,我试图替换整个字符串集合('Names')。

public class NamesUpdate
{
    public List<string> Names { get; } = new List<string>();
}
public void ReplaceNames([FromBody] JsonPatchDocument<NamesUpdate> namesUpdate)
{
    var newNames = new NamesUpdate();
    namesUpdate.ApplyTo(newNames);
}

请求对象:

[
    {
      "op": "replace",
      "path": "/names/",
      "value": ["Ben", "James"]
    }
]

错误(从 ApplyTo 行抛出):

Microsoft.AspNetCore.JsonPatch.Exceptions.JsonPatchException: The property at path 'names' could not be updated.

这个错误很笼统,我觉得请求对象没问题。关于如何替换整个集合的任何想法?

【问题讨论】:

    标签: c# json .net json-patch


    【解决方案1】:

    您没有设置访问器。

    [HttpPatch]
    public IActionResult ReplaceNames([FromBody] JsonPatchDocument<NamesUpdate> namesUpdate)
    {
        var newNames = new NamesUpdate();
        namesUpdate.ApplyTo(newNames);            
        return Ok(newNames);
    }
    public class NamesUpdate
    {
       public List<string> Names { get; set; } = new List<string>();
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 1970-01-01
      • 2021-06-17
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      相关资源
      最近更新 更多