From 9dd1393e02a039b3c1331cb99662515769b8ba30 Mon Sep 17 00:00:00 2001 From: Wade Pickett Date: Thu, 30 Apr 2026 13:07:55 -0700 Subject: [PATCH 1/3] Updates for jsonpatch.md Fixes #36419 - Replace references to T with TModel - Replace "Action method code" section with Minimal APIs PATCH endpoint - Replace "Action method code" section with Minimal APIs PATCH endpoint - Update the "Get the code" section -Update "Additional resources" source code link --- aspnetcore/web-api/jsonpatch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/web-api/jsonpatch.md b/aspnetcore/web-api/jsonpatch.md index d9644898c533..91a6e7af729a 100644 --- a/aspnetcore/web-api/jsonpatch.md +++ b/aspnetcore/web-api/jsonpatch.md @@ -57,7 +57,7 @@ To enable JSON Patch support with , install the [`Microso dotnet add package Microsoft.AspNetCore.JsonPatch.SystemTextJson ``` -This package provides a class to represent a JSON Patch document for objects of type `T` and custom logic for serializing and deserializing JSON Patch documents using . The key method of the class is , which applies the patch operations to a target object of type `T`. +This package provides a class to represent a JSON Patch document for objects of type `TModel` and custom logic for serializing and deserializing JSON Patch documents using . The key method of the class is , which applies the patch operations to a target object of type `TModel`. ## Action method code applying JSON Patch From ed04bb42cb80af4f1dac1825f11106925d3c8c39 Mon Sep 17 00:00:00 2001 From: Wade Pickett Date: Thu, 30 Apr 2026 13:26:35 -0700 Subject: [PATCH 2/3] Apply suggestion from @wadepickett --- aspnetcore/web-api/jsonpatch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/web-api/jsonpatch.md b/aspnetcore/web-api/jsonpatch.md index 91a6e7af729a..ace93b33e81c 100644 --- a/aspnetcore/web-api/jsonpatch.md +++ b/aspnetcore/web-api/jsonpatch.md @@ -5,7 +5,7 @@ description: Learn how to handle JSON Patch requests in an ASP.NET Core web API. monikerRange: '>= aspnetcore-3.1' ms.author: wpickett ms.custom: mvc -ms.date: 06/03/2025 +ms.date: 04/30/2026 uid: web-api/jsonpatch --- # JSON Patch support in ASP.NET Core web API From 48163e4ac5eed0343c1cc8b96cf6370c8a212835 Mon Sep 17 00:00:00 2001 From: wadepickett Date: Thu, 30 Apr 2026 13:56:45 -0700 Subject: [PATCH 3/3] Added new Min API section draft --- aspnetcore/web-api/jsonpatch.md | 47 +++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/aspnetcore/web-api/jsonpatch.md b/aspnetcore/web-api/jsonpatch.md index ace93b33e81c..b6ade1945dd3 100644 --- a/aspnetcore/web-api/jsonpatch.md +++ b/aspnetcore/web-api/jsonpatch.md @@ -1,9 +1,10 @@ --- title: JsonPatch in ASP.NET Core web API author: wadepickett -description: Learn how to handle JSON Patch requests in an ASP.NET Core web API. +description: "JSON Patch in ASP.NET Core web API: Learn how to handle JSON Patch requests, apply partial updates, and improve API efficiency with System.Text.Json." monikerRange: '>= aspnetcore-3.1' ms.author: wpickett +ms.reviewer: wpickett ms.custom: mvc ms.date: 04/30/2026 uid: web-api/jsonpatch @@ -67,9 +68,17 @@ In an API controller, an action method for JSON Patch: * Accepts a , typically with [](xref:Microsoft.AspNetCore.Mvc.FromBodyAttribute). * Calls on the patch document to apply the changes. -### Example Controller Action method: +## Minimal API PATCH endpoint applying JSON Patch -:::code language="csharp" source="~/web-api/jsonpatch/samples/10.x/JsonPatchSample/Controllers/CustomerController.cs" id="snippet_PatchAction" highlight="1,2,14-19"::: +In a Minimal API, a PATCH endpoint for JSON Patch: + +* Uses `MapPatch` to define the route. +* Accepts a parameter. +* Calls on the patch document to apply the changes. + +### Example Minimal API PATCH endpoint: + +:::code language="csharp" source="~/web-api/jsonpatch/samples/10.x/JsonPatchSample/CustomerApi.cs"::: This code from the sample app works with the following `Customer` and `Order` models: @@ -77,29 +86,33 @@ This code from the sample app works with the following `Customer` and `Order` mo :::code language="csharp" source="~/web-api/jsonpatch/samples/10.x/JsonPatchSample/Models/Order.cs"::: -The sample action method's key steps: +The sample PATCH endpoint's key steps: * **Retrieve the Customer**: - * The method retrieves a `Customer` object from the database `AppDb` using the provided id. - * If no `Customer` object is found, it returns a `404 Not Found` response. + * The endpoint retrieves a `Customer` object from the database `AppDb` using the provided `id`. + * If no `Customer` object is found, it returns a `404 Not Found` response via `TypedResults.NotFound()`. * **Apply JSON Patch**: - * The method applies the JSON Patch operations from the patchDoc to the retrieved `Customer` object. - * If errors occur during the patch application, such as invalid operations or conflicts, they are captured by an error handling delegate. This delegate adds error messages to the `ModelState` using the type name of the affected object and the error message. -* **Validate ModelState**: - * After applying the patch, the method checks the `ModelState` for errors. - * If the `ModelState` is invalid, such as due to patch errors, it returns a `400 Bad Request` response with the validation errors. -* **Return the Updated Customer**: - * If the patch is successfully applied and the `ModelState` is valid, the method returns the updated `Customer` object in the response. + * The method applies the JSON Patch operations from the `patchDoc` to the retrieved `Customer` object. + * If errors occur during the patch application, such as invalid operations or conflicts, they are captured by an error handling delegate. This delegate collects error messages into a dictionary keyed by the type name of the affected object. +* **Return validation errors**: + * If any errors were captured during the patch application, the endpoint returns a `ValidationProblem` response containing the error details via `TypedResults.ValidationProblem(errors)`. +* **Save and return the Updated Customer**: + * If the patch is successfully applied with no errors, the changes are saved to the database and the endpoint returns the updated `Customer` object via `TypedResults.Ok(customer)`. ### Example error response: -The following example shows the body of a `400 Bad Request` response for a JSON Patch operation when the specified path is invalid: +The following example shows the body of a validation problem response for a JSON Patch operation when the specified path is invalid: ```json { - "Customer": [ - "The target location specified by path segment 'foobar' was not found." - ] + "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1", + "title": "One or more validation errors occurred.", + "status": 400, + "errors": { + "Customer": [ + "The target location specified by path segment 'foobar' was not found." + ] + } } ```