Patch REST API to Partial Update MongoDB in .NET

前端 未结 4 1156
情书的邮戳
情书的邮戳 2021-02-11 02:22

I have an object

{
  \"_id\": \"testobject\",
  \"A\": \"First line\",
  \"B\": \"Second line\",
  \"C\": \"Third line\"
}

I want to send a RE

4条回答
  •  走了就别回头了
    2021-02-11 02:40

    In ASP.net core 3.1 you can use the JsonPatchDocument and do a replace in mongodb

    JsonPatch in ASP.NET Core web API

    [HttpPatch]
    public IActionResult JsonPatchWithModelState(
        [FromBody] JsonPatchDocument patchDoc)
    {
        if (patchDoc != null)
        {
            var customer = CreateCustomer();
    
            patchDoc.ApplyTo(customer, ModelState);
    
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
    
            return new ObjectResult(customer);
        }
        else
        {
            return BadRequest(ModelState);
        }
    }
    

提交回复
热议问题