You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/call-web-api.md
+25-11Lines changed: 25 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
-
title: Call a web API from ASP.NET Core Blazor
2
+
title: Call a web API from ASP.NET Core Blazor WebAssembly
3
3
author: guardrex
4
-
description: Learn how to call a web API from a Blazor app using JSON helpers, including making cross-origin resource sharing (CORS) requests.
4
+
description: Learn how to call a web API from a Blazor WebAssembly app using JSON helpers, including making cross-origin resource sharing (CORS) requests.
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: riande
7
7
ms.custom: mvc
8
-
ms.date: 01/22/2020
8
+
ms.date: 04/16/2020
9
9
no-loc: [Blazor, SignalR]
10
10
uid: blazor/call-web-api
11
11
---
@@ -28,9 +28,19 @@ See the following components in the *BlazorWebAssemblySample* sample app:
28
28
29
29
## Packages
30
30
31
-
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/).
31
+
Reference the [System.Net.Http.Json](https://www.nuget.org/packages/System.Net.Http.Json/) NuGet package in the project file.
32
32
33
-
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.
33
+
## Add the HttpClient service
34
+
35
+
In `Program.Main`, add an `HttpClient` service if it doesn't already exist:
JSON helper methods send requests to a URI (a web API in the following examples) and process the response:
66
76
67
-
*`GetJsonAsync`– Sends an HTTP GET request and parses the JSON response body to create an object.
77
+
*`GetFromJsonAsync`– Sends an HTTP GET request and parses the JSON response body to create an object.
68
78
69
79
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.
70
80
@@ -76,11 +86,11 @@ JSON helper methods send requests to a URI (a web API in the following examples)
*`PostJsonAsync`– Sends an HTTP POST request, including JSON-encoded content, and parses the JSON response body to create an object.
93
+
*`PostAsJsonAsync`– Sends an HTTP POST request, including JSON-encoded content, and parses the JSON response body to create an object.
84
94
85
95
In the following code, `_newItemName` is provided by a bound element of the component. The `AddItem` method is triggered by selecting a `<button>` element. See the sample app for a complete example.
86
96
@@ -97,12 +107,14 @@ JSON helper methods send requests to a URI (a web API in the following examples)
97
107
private async Task AddItem()
98
108
{
99
109
var addItem = new TodoItem { Name = _newItemName, IsComplete = false };
Calls to `PostAsJsonAsync` return an <xref:System.Net.Http.HttpResponseMessage>.
104
116
105
-
*`PutJsonAsync`– Sends an HTTP PUT request, including JSON-encoded content.
117
+
*`PutAsJsonAsync`– Sends an HTTP PUT request, including JSON-encoded content.
106
118
107
119
In the following code, `_editItem` values for `Name` and `IsCompleted` are provided by bound elements of the component. The item's `Id` is set when the item is selected in another part of the UI and `EditItem` is called. The `SaveItem` method is triggered by selecting the Save `<button>` element. See the sample app for a complete example.
108
120
@@ -125,9 +137,11 @@ JSON helper methods send requests to a URI (a web API in the following examples)
Calls to `PutAsJsonAsync` return an <xref:System.Net.Http.HttpResponseMessage>.
131
145
132
146
<xref:System.Net.Http> includes additional extension methods for sending HTTP requests and receiving HTTP responses. [HttpClient.DeleteAsync](xref:System.Net.Http.HttpClient.DeleteAsync*) is used to send an HTTP DELETE request to a web API.
0 commit comments