Skip to content

Commit f855631

Browse files
authored
Add JSON raw string literal example (#34908)
1 parent 432bb41 commit f855631

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,24 @@ await Http.PatchAsJsonAsync(
505505
"[{\"operationType\":2,\"path\":\"/IsComplete\",\"op\":\"replace\",\"value\":true}]");
506506
```
507507

508+
As of C# 11 (.NET 7), you can compose a JSON string as a [raw string literal](/dotnet/csharp/language-reference/tokens/raw-string). Specify JSON syntax with the <xref:System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.Json%2A?displayProperty=nameWithType> field to the [`[StringSyntax]` attribute](xref:System.Diagnostics.CodeAnalysis.StringSyntaxAttribute) for code analysis tooling:
509+
510+
```razor
511+
@using System.Diagnostics.CodeAnalysis
512+
513+
...
514+
515+
@code {
516+
[StringSyntax(StringSyntaxAttribute.Json)]
517+
private const string patchOperation =
518+
"""[{"operationType":2,"path":"/IsComplete","op":"replace","value":true}]""";
519+
520+
...
521+
522+
await Http.PatchAsJsonAsync($"todoitems/{id}", patchOperation);
523+
}
524+
```
525+
508526
<xref:System.Net.Http.Json.HttpClientJsonExtensions.PatchAsJsonAsync%2A> returns an <xref:System.Net.Http.HttpResponseMessage>. To deserialize the JSON content from the response message, use the <xref:System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync%2A> extension method. The following example reads JSON todo item data as an array. An empty array is created if no item data is returned by the method, so `content` isn't null after the statement executes:
509527

510528
```csharp

0 commit comments

Comments
 (0)