JsonPatchSupport.AspNetCore is a lightweight package that enables JSON Patch support in ASP.NET Core using the Newtonsoft.Json library, allowing simple partial updates with minimal configuration.
For more details, visit the related blog post: ASP.NET Core JSON Patch API Example (written in Traditional Chinese).
Install the package via .NET CLI:
dotnet add package JsonPatchSupport.AspNetCoreAlternatively, add a project reference to your ASP.NET Core application.
Register Services
Add the auto service registration extension in your service configuration:
// add using directive
using JsonPatchSupport.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// register service
builder.Services.AddJsonPatchSupport();
var app = builder.Build();Below is an example of how to use JSON Patch in an API controller:
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
[HttpPatch("{id}")]
public IActionResult Patch([FromRoute] int id, [FromBody] JsonPatchDocument<ProductDto> patchDocument)
{
// Simulate getting the entity from a data source
var product = new ProductDto
{
Name = "product1",
Price = 10,
};
// Apply the patch to the product
patchDocument.ApplyTo(product, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Simulate saving the updated entity to a data source
return Ok(product);
}
}
public class ProductDto
{
public string Name { get; set; }
public int Price { get; set; }
}Example JSON Patch document:
[
{
"op": "replace",
"path": "/Name",
"value": "NewProductName"
},
{
"op": "replace",
"path": "/Price",
"value": "99"
}
]Example cURL request:
curl -X 'PATCH' \
'http://localhost:5183/api/Product/1' \
-H 'accept: */*' \
-H 'Content-Type: application/json-patch+json' \
-d '
[
{
"op": "replace",
"path": "/Name",
"value": "NewProductName"
},
{
"op": "replace",
"path": "/Price",
"value": "99"
}
]'Expected response:
{
"name": "NewProductName",
"price": 99
}