【问题标题】:(.net core 3.1 & Angular) Json Patch: The path segment is invalid for an array index(.net core 3.1 和 Angular)Json 补丁:路径段对数组索引无效
【发布时间】:2020-07-31 13:33:52
【问题描述】:

我有一个json文件,内容如下:

{
    "Id": 0,
    "Name": "todoList1",
    "ListItems": [
        {
            "Id": 1,
            "Description": "listItem1.1",
            "Done": false
        },
        {
            "Id": 2,
            "Description": "listItem1.2",
            "Done": true
        }
    ]
}

此外,我还有一个 ASP.NET Core 3.1 API 方法,它应该能够部分更新 json:

 [HttpPatch("{name}")]
    public async Task<TodoListDomainModel> Update([FromRoute] string name, [FromBody]JsonPatchDocument<TodoListDomainModel> todoListJsonPatchDocument)
    {
        var todoListToPatch = (await this._todoListService.GetTodoLists()).Single(x => x.Name == name);
        todoListJsonPatchDocument.ApplyTo(todoListToPatch);
        return todoListToPatch;
    }

TodoListDomainModel:

public class TodoListDomainModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<ListItemDomainModel> ListItems { get; set; } = new Collection<ListItemDomainModel>();
}

ListItemDomainModel

public class ListItemDomainModel
{
    public ListItemDomainModel()
    {
    }

    public int Id { get; set;  }

    public string Description { get; set; }

    public bool Done { get; set; }

    public DateTime DueDate { get; set; }
}

通过 Angular 调用 API 方法

通过angular调用API方法时:

save(selectedTodoList: TodoList) {
    let test = JSON.stringify(selectedTodoList.listItems);
    let json = `[{"op": "replace", "path": "/ListItems", "value": ${test} }]`;
    let url = `https://localhost:44305/todolist/${selectedTodoList.name}`;
    return this.http.patch<TodoList>(url, json);
  }

我什至没有到达 api 方法中的断点。

是不是我做错了什么?

提前致谢

【问题讨论】:

  • 组合 JSON 正文的错误方法:字符串可能导致格式错误(这就是您的问题的嫌疑人)。将 JSON 构建为 JavaScript 对象,并按原样交付。
  • 将 JSON 构建为 JS 对象是什么意思?抱歉,我是前端的新手 :)
  • 控制器中的TodoListDomainModel 是什么?
  • @YiyiYou:TodoListDomainModel 看起来像这样: public class TodoListDomainModel { public int Id { get;放; } 公共字符串名称 { 获取;放; } 公共 ICollection ListItems { 获取;放; } = 新集合(); }
  • @xeraphim 看看下面我的回答

标签: angular http asp.net-core asp.net-core-3.1 http-patch


【解决方案1】:

尝试将 JSON 主体组合为 JavaScript 对象:

save(selectedTodoList: TodoList) {
    const body = [{
        op: "replace",
        path: "/ListItems",
        value: selectedTodoList.listItems
    }];
    let url = `https://localhost:44305/todolist/${selectedTodoList.name}`;
    return this.http.patch<TodoList>(url, body);
}

【讨论】:

  • 我在http.patch 的末尾添加了一个.subscribe()。不幸的是,我现在收到以下错误:error: Object { type: "https://tools.ietf.org/html/rfc7231#section-6.5.1", title: "One or more validation errors occurred.", status: 400, … }。这怎么可能?我什至没有任何验证:)
  • 上面写着The JSON patch document was malformed and could not be parsed 好像用这种方式创建身体还是有问题:(
  • 是的,但这是一个好兆头:这意味着 JSON 已发送并到达后端。现在的问题是 C# 类与传入的 JSON 不匹配。
  • 使用浏览器开发工具(Chrome 为 F12),分析流经 HTTP 补丁请求的实际正文是什么。
  • 嗯,是的。我不知道JsonPatchDocument 是什么,但它似乎是集合的包装器,因此数组可能是要发送的正确对象。请注意,我已经用方括号包裹了 body 常量。
猜你喜欢
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2020-05-20
  • 2018-03-05
  • 1970-01-01
相关资源
最近更新 更多