JsonPatchSupport.AspNetCore 1.0.1

dotnet add package JsonPatchSupport.AspNetCore --version 1.0.1
                    
NuGet\Install-Package JsonPatchSupport.AspNetCore -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsonPatchSupport.AspNetCore" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JsonPatchSupport.AspNetCore" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="JsonPatchSupport.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JsonPatchSupport.AspNetCore --version 1.0.1
                    
#r "nuget: JsonPatchSupport.AspNetCore, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JsonPatchSupport.AspNetCore@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JsonPatchSupport.AspNetCore&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=JsonPatchSupport.AspNetCore&version=1.0.1
                    
Install as a Cake Tool

JsonPatchSupport.AspNetCore

NuGet version (JsonPatchSupport.AspNetCore) GitHub Actions Workflow Status

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 257 2/19/2025
1.0.0 199 2/18/2025