Skip to content

Commit 175a38d

Browse files
committed
Moved Action Method code section back in
1 parent 4a9dc04 commit 175a38d

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

aspnetcore/web-api/jsonpatch.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,45 @@ dotnet add package Microsoft.AspNetCore.JsonPatch.SystemTextJson --prerelease
5454

5555
This package provides a `JsonPatchDocument<T>` class to represent a JSON Patch document for objects of type `T` and custom logic for serializing and deserializing JSON Patch documents using `System.Text.Json`. The key method of the `JsonPatchDocument<T>` class is `ApplyTo`, which applies the patch operations to a target object of type `T`.
5656

57+
## Action method code
58+
59+
In an API controller, an action method for JSON Patch:
60+
61+
* Is annotated with the `HttpPatch` attribute.
62+
* Accepts a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601>, typically with [`[FromBody]`](xref:Microsoft.AspNetCore.Mvc.FromBodyAttribute).
63+
* Calls <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> on the patch document to apply the changes.
64+
65+
Here's an example:
66+
67+
:::code language="csharp" source="~/web-api/jsonpatch/samples/3.x/api/Controllers/HomeController.cs" id="snippet_PatchAction" highlight="1,3,9":::
68+
69+
This code from the sample app works with the following `Customer` model:
70+
71+
:::code language="csharp" source="~/web-api/jsonpatch/samples/6.x/api/Models/Customer.cs":::
72+
73+
:::code language="csharp" source="~/web-api/jsonpatch/samples/6.x/api/Models/Order.cs":::
74+
75+
The sample action method:
76+
77+
* Constructs a `Customer`.
78+
* Applies the patch.
79+
* Returns the result in the body of the response.
80+
81+
> [!NOTE]
82+
> In a real app, the data would typically be retrieved from a store such as a database. After applying the patch, the updated data would be saved back to the database.
83+
84+
### Model state
85+
86+
The preceding action method example calls an overload of `ApplyTo` that takes model state as one of its parameters. With this option, you can get error messages in responses. The following example shows the body of a 400 Bad Request response for a `test` operation:
87+
88+
```json
89+
{
90+
"Customer": [
91+
"The current value 'John' at path 'customerName' != test value 'Nancy'."
92+
]
93+
}
94+
```
95+
5796
## JSON Patch Operations
5897

5998
The following sections describe the supported JSON Patch operations `add`, `remove`, `replace`, `move`, `copy`, and `test` that modify a JSON document and provide examples of their usage.
@@ -202,7 +241,7 @@ Example:
202241

203242
The following examples demonstrate how to use the `ApplyTo` method to apply a JSON Patch document to an object.
204243

205-
## Example: Apply a `JsonPatchDocument` to an object
244+
### Example: Apply a `JsonPatchDocument` to an object
206245

207246
The following example demonstrates:
208247

@@ -282,7 +321,7 @@ Key differences between `System.Text.Json` and the new `JsonPatchDocument<T>` im
282321
* The runtime type of the target object, not the declared type, determines which properties `ApplyTo` patches.
283322
* `System.Text.Json` deserialization relies on the declared type to identify eligible properties.
284323

285-
## Example: Apply a JsonPatchDocument with error handling
324+
### Example: Apply a JsonPatchDocument with error handling
286325

287326
There are various errors that can occur when applying a JSON Patch document. For example, the target object may not have the specified property, or the value specified might be incompatible with the property type.
288327

@@ -404,6 +443,17 @@ public void Validate(JsonPatchDocument<T> patch)
404443
* Protect endpoints accepting JSON Patch requests with proper authentication and authorization mechanisms.
405444
* Restrict access to trusted clients or users with appropriate permissions.
406445

446+
## Get the code
447+
448+
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/web-api/jsonpatch/samples). ([How to download](xref:index#how-to-download-a-sample)).
449+
450+
To test the sample, run the app and send HTTP requests with the following settings:
451+
452+
* URL: `http://localhost:{port}/jsonpatch/jsonpatchwithmodelstate`
453+
* HTTP method: `PATCH`
454+
* Header: `Content-Type: application/json-patch+json`
455+
* Body: Copy and paste one of the JSON patch document samples from the *JSON* project folder.
456+
407457
## Additional resources
408458

409459
* [IETF RFC 5789 PATCH method specification](https://tools.ietf.org/html/rfc5789)

0 commit comments

Comments
 (0)