【发布时间】:2019-04-01 10:24:16
【问题描述】:
我想使用 JsonPatchDocument 来更新在 Angular 6 前端更改的模型。 不幸的是,我不断收到带有以下消息的 400 Bad Request 响应:
{"":["The input was not valid."]}
现在我不确定我是否正确执行此操作,但这就是我的代码设置方式:
前端:
edit.ts class
onSubmit() {
this.testService.update(this.id, this.prepareFormModel())
.subscribe(res => console.info(res);
}
prepareFormModel() {
const formModel = this.testForm.value;
const retVal: any = {
title: formModel.title as string,
comment: formModel.comment ? formModel.comment : '' as string,
qualified: formModel.qualified as boolean
};
return retVal;
}
test.service.ts 类
constructor(private http: HttpClient) { }
update(id: string, value: any): Observable<any> {
return this.http.patch<any>('http://localhost:5001/api/test' + '/' + id, value);
}
在我的 ASP CORE 项目测试控制器中
[HttpPatch("{id}")]
public async Task<IActionResult> UpdateModel(Guid id, [FromBody]JsonPatchDocument<TestModel> modelDocument)
{
return Ok();
}
DTO 模型
public class TestModel
{
public string Title { get; set; }
public string Comment { get; set; }
public bool Qualified { get; set; }
}
知道我在塞什么吗?
更新 1:
我注意到 httpclient 补丁只发送内容类型 application/json。查看 JsonPatchDocument 的一些示例,它似乎请求 application/json-patch+json 类型。
【问题讨论】:
-
您是否尝试过更改大小写以使它们匹配?即,都使用
title或都使用Title -
是的,也进行了测试
-
您是否尝试过将 HTTP 正文复制到 Postman 中以先测试 API?更好地使用 'console.log(value);'检查输入。
标签: angular asp.net-core