JsonPatchSupport.AspNetCore
1.0.1
dotnet add package JsonPatchSupport.AspNetCore --version 1.0.1
NuGet\Install-Package JsonPatchSupport.AspNetCore -Version 1.0.1
<PackageReference Include="JsonPatchSupport.AspNetCore" Version="1.0.1" />
<PackageVersion Include="JsonPatchSupport.AspNetCore" Version="1.0.1" />
<PackageReference Include="JsonPatchSupport.AspNetCore" />
paket add JsonPatchSupport.AspNetCore --version 1.0.1
#r "nuget: JsonPatchSupport.AspNetCore, 1.0.1"
#:package JsonPatchSupport.AspNetCore@1.0.1
#addin nuget:?package=JsonPatchSupport.AspNetCore&version=1.0.1
#tool nuget:?package=JsonPatchSupport.AspNetCore&version=1.0.1
JsonPatchSupport.AspNetCore
Overview
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).
Installation
Install the package via .NET CLI:
dotnet add package JsonPatchSupport.AspNetCore
Alternatively, add a project reference to your ASP.NET Core application.
Usage
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();
API Example
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
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 6.0.36)
-
net8.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 8.0.13)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.