【发布时间】: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