https://github.com/ruyut/jsonpatchsupport.aspnetcore
Lightweight ASP.NET Core package integrating JSON Patch support via Newtonsoft.Json for efficient partial updates.
https://github.com/ruyut/jsonpatchsupport.aspnetcore
api asp-net-core csharp csharp-library json
Last synced: 3 months ago
JSON representation
Lightweight ASP.NET Core package integrating JSON Patch support via Newtonsoft.Json for efficient partial updates.
- Host: GitHub
- URL: https://github.com/ruyut/jsonpatchsupport.aspnetcore
- Owner: ruyut
- Created: 2025-02-18T12:23:58.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-02-19T07:50:34.000Z (over 1 year ago)
- Last Synced: 2025-02-19T08:31:18.757Z (over 1 year ago)
- Topics: api, asp-net-core, csharp, csharp-library, json
- Language: C#
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JsonPatchSupport.AspNetCore
[](https://www.nuget.org/packages/JsonPatchSupport.AspNetCore)
[](https://github.com/ruyut/JsonPatchSupport.AspNetCore/actions/workflows/publish.yml)
## 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](https://www.ruyut.com/2025/02/aspnet-core-json-patch-api.html)
(written in Traditional Chinese).
## Installation
Install the package via .NET CLI:
```bash
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:
```csharp
// 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:
```csharp
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 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:
```json
[
{
"op": "replace",
"path": "/Name",
"value": "NewProductName"
},
{
"op": "replace",
"path": "/Price",
"value": "99"
}
]
```
Example cURL request:
```shell
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:
```text
{
"name": "NewProductName",
"price": 99
}
```