Updating specific model fields with PATCH and JsonPatchDocument
Using a PATCH request, we can update specific fields of a data entity (resource) without sending the entire object to the endpoint. In this recipe, we will leverage the JsonPatchDocument library to target and modify only the fields you need to change. This approach ensures minimal data transfer and precise updates to your resources.
Getting ready
The code for the starter project resides here: chapter02\start\JsonPatchDocument. This recipe picks up where the previous one left off.
How to do it…
- First, install the required packages:
dotnet add package Microsoft.AspNetCore.JsonPatch dotnet add package Microsoft.AspNetCore.Mvc.NewtonSoftJson
We are not going to use
System.Text.Jsonas our JSON provider in this example. Although it is possible to set upJsonPatchwithSystem.Text.Json, it is more work. Import theMvc.Formattersnamespace and register that you would like to useNewtonsoftforJsonon our...