From ef6b095976f6e7387e8f4438113464149778e59e Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Thu, 16 Apr 2020 19:47:34 -0500 Subject: [PATCH] [Preview 4] Update Blazor HttpClient coverage (#17861) --- aspnetcore/blazor/call-web-api.md | 36 +++++++++++++++++++++---------- aspnetcore/toc.yml | 2 +- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/aspnetcore/blazor/call-web-api.md b/aspnetcore/blazor/call-web-api.md index 18bf3f9225..67a6ed35ef 100644 --- a/aspnetcore/blazor/call-web-api.md +++ b/aspnetcore/blazor/call-web-api.md @@ -1,11 +1,11 @@ --- -title: Call a web API from ASP.NET Core Blazor +title: Call a web API from ASP.NET Core Blazor WebAssembly author: guardrex -description: Learn how to call a web API from a Blazor app using JSON helpers, including making cross-origin resource sharing (CORS) requests. +description: Learn how to call a web API from a Blazor WebAssembly app using JSON helpers, including making cross-origin resource sharing (CORS) requests. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 01/22/2020 +ms.date: 04/16/2020 no-loc: [Blazor, SignalR] uid: blazor/call-web-api --- @@ -28,9 +28,19 @@ See the following components in the *BlazorWebAssemblySample* sample app: ## Packages -Reference the *experimental* [Microsoft.AspNetCore.Blazor.HttpClient](https://www.nuget.org/packages/Microsoft.AspNetCore.Blazor.HttpClient/) NuGet package in the project file. `Microsoft.AspNetCore.Blazor.HttpClient` is based on `HttpClient` and [System.Text.Json](https://www.nuget.org/packages/System.Text.Json/). +Reference the [System.Net.Http.Json](https://www.nuget.org/packages/System.Net.Http.Json/) NuGet package in the project file. -To use a stable API, use the [Microsoft.AspNet.WebApi.Client](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/) package, which uses [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/)/[Json.NET](https://www.newtonsoft.com/json/help/html/Introduction.htm). Using the stable API in `Microsoft.AspNet.WebApi.Client` doesn't provide the JSON helpers described in this topic, which are unique to the experimental `Microsoft.AspNetCore.Blazor.HttpClient` package. +## Add the HttpClient service + +In `Program.Main`, add an `HttpClient` service if it doesn't already exist: + +```csharp +builder.Services.AddSingleton( + new HttpClient + { + BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) + }); +``` ## HttpClient and JSON helpers @@ -64,7 +74,7 @@ private class TodoItem JSON helper methods send requests to a URI (a web API in the following examples) and process the response: -* `GetJsonAsync` – Sends an HTTP GET request and parses the JSON response body to create an object. +* `GetFromJsonAsync` – Sends an HTTP GET request and parses the JSON response body to create an object. In the following code, the `_todoItems` are displayed by the component. The `GetTodoItems` method is triggered when the component is finished rendering ([OnInitializedAsync](xref:blazor/lifecycle#component-initialization-methods)). See the sample app for a complete example. @@ -76,11 +86,11 @@ JSON helper methods send requests to a URI (a web API in the following examples) private TodoItem[] _todoItems; protected override async Task OnInitializedAsync() => - _todoItems = await Http.GetJsonAsync("api/TodoItems"); + _todoItems = await Http.GetFromJsonAsync("api/TodoItems"); } ``` -* `PostJsonAsync` – Sends an HTTP POST request, including JSON-encoded content, and parses the JSON response body to create an object. +* `PostAsJsonAsync` – Sends an HTTP POST request, including JSON-encoded content, and parses the JSON response body to create an object. In the following code, `_newItemName` is provided by a bound element of the component. The `AddItem` method is triggered by selecting a `