代码之家  ›  专栏  ›  技术社区  ›  Aeseir

ASP核心修补程序文档返回无效输入

  •  1
  • Aeseir  · 技术社区  · 7 年前

    我想使用JsonPatchDocument来更新在angular6前端更改的模型。

    {"":["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;
      }
    

    constructor(private http: HttpClient) { }
    
      update(id: string, value: any): Observable<any> {
        return this.http.patch<any>('http://localhost:5001/api/test' + '/' + id, value);
      }
    

    在我的ASP核心项目测试控制器中

        [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; } 
        }
    

    你知道我在塞什么东西吗?

    我注意到httpclient补丁只发送内容类型application/json。看看JsonPatchDocument的一些例子,它似乎请求application/json patch+json类型。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Edward    7 年前

    JsonPatchDocument 它用于准确描述如何修改文档(例如,用另一个值替换字段中的值),而不必同时发送其余未更改的值。

    您当前正在通过 prepareFormModel 而不是描述如何修改 formModel

    TestModel 直接在 UpdateModel ,您需要删除

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

    如果你想实施 这在 JSON Patch With ASP.net Core fast-json-patch .

    • 美国石油学会

          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 ,您将需要实现以下内容才能获得

    [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;
    }
    }
    
    推荐文章