我想使用JsonPatchDocument更新在Angular 6前端上更改的模型。 不幸的是,我不断收到400错误请求响应,并显示以下消息:
{"":["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类型。
答案 0 :(得分:1)
对于您当前的代码,您误解了JsonPatchDocument
,该词用于准确描述您要如何修改文档(例如,用另一个值替换字段中的值),而不必同时发送其余内容不变值。
您当前正在传递prepareFormModel
,而不是描述您要如何修改formModel
。
如果您想直接在TestModel
中获得UpdateModel
,则需要删除JsonPatchDocument
。
public async Task<IActionResult> UpdateModelWithOutJsonPatch(Guid id, [FromBody]TestModel modelDocument)
{
return Ok();
}
如果要实现JSON Patch With ASP.net Core中描述的JsonPatchDocument
,则需要传递文档描述,对于json路径库,可以尝试fast-json-patch
。
API
public async Task<IActionResult> UpdateModelWithJsonPatch(Guid id, [FromBody]JsonPatchDocument<TestModel> modelDocument)
{
return Ok();
}
安装软件包
npm install fast-json-patch --save
导入功能
import { compare } from 'fast-json-patch';
比较对象并传递eh jsonpatch对象。
export class FetchDataComponent {
public forecasts: WeatherForecast[];
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
const patch = compare(this.previousFormModel(), this.prepareFormModel());
http.patch<any>(baseUrl + 'api/SampleData/UpdateModelWithJsonPatch/1', patch).subscribe(result => {
console.log(result);
}, error => console.error(error));;
http.patch<any>(baseUrl + 'api/SampleData/UpdateModelWithOutJsonPatch/1', this.prepareFormModel()).subscribe(result => {
console.log(result);
}, error => console.error(error));;
}
previousFormModel() {
//const formModel = this.testForm.value;
const retVal: any = {
title: "t2" as string,
comment: "c2" as string,
qualified: false as boolean
};
return retVal;
}
prepareFormModel() { //const formModel = this.testForm.value;
const retVal: any = {
title: "t1" as string,
comment: "c1" as string,
qualified: true as boolean
};
return retVal;
}
}
注意,对于JsonPatch
,您将需要执行以下类似操作才能获得TestModel
[Route("api/[controller]")]
public class PersonController : Controller
{
private readonly Person _defaultPerson = new Person
{
FirstName = "Jim",
LastName = "Smith"
};
[HttpPatch("update")]
public Person Patch([FromBody]JsonPatchDocument<Person> personPatch)
{
personPatch.ApplyTo(_defaultPerson);
return _defaultPerson;
}
}