Angular ASP核心修补程序文档返回无效输入
我想使用JsonPatchDocument来更新Angular 6前端更改的模型。 不幸的是,我一直收到400个错误的请求响应,并显示以下消息:Angular ASP核心修补程序文档返回无效输入,angular,asp.net-core,Angular,Asp.net Core,我想使用JsonPatchDocument来更新Angular 6前端更改的模型。 不幸的是,我一直收到400个错误的请求响应,并显示以下消息: {"":["The input was not valid."]} 现在我不确定我是否正确执行了此操作,但我的代码就是这样设置的: 前端: edit.ts class onSubmit() { this.testService.update(this.id, this.prepareFormModel()) .subscribe
{"":["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);
}
你知道我在搞什么吗
更新1:
我注意到httpclient补丁只发送内容类型application/json。查看JsonPatchDocument的一些示例,它似乎请求应用程序/json patch+json类型。对于您当前的代码,您误解了
JsonPatchDocument
,它用于准确描述您希望如何修改文档(例如,用另一个值替换字段中的值)无需同时发送其余未更改的值
您当前正在传递prepareFormModel
,而不是描述如何修改formModel
如果您想直接在UpdateModel
中获取TestModel
,则需要删除JsonPatchDocument
public async Task<IActionResult> UpdateModelWithOutJsonPatch(Guid id, [FromBody]TestModel modelDocument)
{
return Ok();
}
npm install fast-json-patch --save
import { compare } from 'fast-json-patch';
导出类FetchDataComponent{
公众预报:天气预报[];
构造函数(http:HttpClient,@Inject('BASE_URL')baseUrl:string){
const patch=compare(this.previousFormModel(),this.prepareFormModel());
http.patch(baseUrl+'api/SampleData/UpdateModelWithJsonPatch/1',patch).subscribe(结果=>{
控制台日志(结果);
},error=>console.error(error));;
http.patch(baseUrl+'api/SampleData/UpdateModelWithOutJsonPatch/1',this.prepareFormModel()).subscribe(结果=>{
控制台日志(结果);
},error=>console.error(error));;
}
previousFormModel(){
//const formModel=this.testForm.value;
常数返回:任意={
标题:“t2”作为字符串,
注释:“c2”作为字符串,
限定:false为布尔值
};
返回返回;
}
prepareFormModel(){//const formModel=this.testForm.value;
常数返回:任意={
标题:“t1”作为字符串,
注释:“c1”作为字符串,
限定:true为布尔值
};
返回返回;
}
}
您是否尝试过更改案例以使其匹配?i、 例如,两者都使用title
或两者都使用title
是的,您是否也尝试过将HTTP正文复制到Postman中以首先测试API?更好地使用“console.log(value);”检查输入。
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';