Skip to content

Commit 1c5b676

Browse files
authored
[Pre4, Blazor only] New PATCH w/System.Text.Json (#35456)
1 parent 77f6e45 commit 1c5b676

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,35 @@ Laid out with indentation, spacing, and unescaped quotes, the unencoded PATCH do
676676

677677
To simplify the creation of PATCH documents in the app issuing PATCH requests, an app can use .NET JSON PATCH support, as the following guidance demonstrates.
678678

679+
:::moniker-end
680+
681+
:::moniker range=">= aspnetcore-10.0"
682+
683+
<!-- UPDATE 10.0 - API doc cross-link -->
684+
685+
Install the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch.SystemTextJson) NuGet package and use the API features of the package to compose a `JsonPatchDocument` for a PATCH request.
686+
687+
[!INCLUDE[](~/includes/package-reference.md)]
688+
689+
Add `@using` directives for the <xref:System.Text.Json?displayProperty=fullName>, <xref:System.Text.Json.Serialization?displayProperty=fullName>, and `Microsoft.AspNetCore.JsonPatch.SystemTextJson` <!-- <xref:Microsoft.AspNetCore.JsonPatch.SystemTextJson?displayProperty=fullName> --> namespaces to the top of the Razor component:
690+
691+
```razor
692+
@using System.Text.Json
693+
@using System.Text.Json.Serialization
694+
@using Microsoft.AspNetCore.JsonPatch.SystemTextJson
695+
```
696+
697+
Compose the `JsonPatchDocument` for a `TodoItem` with `IsComplete` set to `true` using the `JsonPatchDocument.Replace` method:
698+
699+
```csharp
700+
var patchDocument = new JsonPatchDocument<TodoItem>()
701+
.Replace(p => p.IsComplete, true);
702+
```
703+
704+
:::moniker-end
705+
706+
:::moniker range=">= aspnetcore-7.0 < aspnetcore-10.0"
707+
679708
Install the [`Microsoft.AspNetCore.JsonPatch`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch) NuGet package and use the API features of the package to compose a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument> for a PATCH request.
680709

681710
[!INCLUDE[](~/includes/package-reference.md)]
@@ -695,6 +724,10 @@ var patchDocument = new JsonPatchDocument<TodoItem>()
695724
.Replace(p => p.IsComplete, true);
696725
```
697726

727+
:::moniker-end
728+
729+
:::moniker range=">= aspnetcore-7.0"
730+
698731
Pass the document's operations (`patchDocument.Operations`) to the <xref:System.Net.Http.Json.HttpClientJsonExtensions.PatchAsJsonAsync%2A> call:
699732

700733
```csharp
@@ -716,6 +749,24 @@ Add <xref:System.Text.Json.JsonSerializerOptions.WriteIndented?displayProperty=n
716749

717750
Follow the guidance in the <xref:web-api/jsonpatch> article to add a PATCH controller action to the web API. Alternatively, PATCH request processing can be implemented as a [Minimal API](xref:fundamentals/minimal-apis) with the following steps.
718751

752+
:::moniker-end
753+
754+
:::moniker range=">= aspnetcore-10.0"
755+
756+
<!-- UPDATE 10.0 - API doc cross-link -->
757+
758+
Add a package reference for the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson) NuGet package to the web API app.
759+
760+
In the `Program` file add an `@using` directive for the `Microsoft.AspNetCore.JsonPatch.SystemTextJson` <!-- <xref:Microsoft.AspNetCore.JsonPatch.SystemTextJson?displayProperty=fullName> --> namespace:
761+
762+
```csharp
763+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson;
764+
```
765+
766+
:::moniker-end
767+
768+
:::moniker range=">= aspnetcore-7.0 < aspnetcore-10.0"
769+
719770
Add a package reference for the [`Microsoft.AspNetCore.Mvc.NewtonsoftJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson) NuGet package to the web API app.
720771

721772
> [!NOTE]
@@ -727,6 +778,10 @@ In the `Program` file add an `@using` directive for the <xref:Microsoft.AspNetCo
727778
using Microsoft.AspNetCore.JsonPatch;
728779
```
729780

781+
:::moniker-end
782+
783+
:::moniker range=">= aspnetcore-7.0"
784+
730785
Provide the endpoint to the request processing pipeline of the web API:
731786

732787
```csharp

0 commit comments

Comments
 (0)