【问题标题】:ASP Core PatchDocument returning Invalid InputASP Core PatchDocument 返回无效输入
【发布时间】: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


【解决方案1】:

对于您当前的代码,您误解了JsonPatchDocument,它用于准确描述您希望如何修改文档(例如,将字段中的值替换为另一个值),而不必同时发送其余部分不变的值。

您当前传递的是prepareFormModel,而不是描述您要如何修改formModel

如果要直接在UpdateModel中获取TestModel,则需要去掉JsonPatchDocument

    public async Task<IActionResult> UpdateModelWithOutJsonPatch(Guid id, [FromBody]TestModel modelDocument)
    {
        return Ok();
    }

如果你想实现JsonPatchDocument,描述在JSON Patch With ASP.net Core,你需要传递文档描述,对于一个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;
}
}

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 2019-03-02
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多