(.net core 3.1 & Angular) Json 补丁:路径段对于数组索引无效
(.net core 3.1 & Angular) Json Patch: The path segment is invalid for an array index
我有一个包含以下内容的 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 主体组合为 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);
}
我有一个包含以下内容的 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 主体组合为 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);
}