Skip to content

Commit ec9d5af

Browse files
committed
Updates
1 parent e06d4f8 commit ec9d5af

File tree

3 files changed

+288
-32
lines changed

3 files changed

+288
-32
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to call a web API from Blazor apps.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 03/08/2024
8+
ms.date: 09/25/2024
99
uid: blazor/call-web-api
1010
---
1111
# Call a web API from ASP.NET Core Blazor
@@ -242,6 +242,37 @@ You can address this by flowing prerendered state using the Persistent Component
242242

243243
:::moniker-end
244244

245+
:::moniker range=">= aspnetcore-9.0"
246+
247+
## Client-side request streaming
248+
249+
For Chromium-based browsers (for example, Google Chrome and Microsoft Edge) using the HTTP/2 protocol, HTTPS, and [CORS](xref:security/cors), client-side Blazor uses [Streams API](https://developer.mozilla.org/docs/Web/API/Streams_API) to permit [request streaming](https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests).
250+
251+
To enable request streaming, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestStreamingEnabled%2A> to `true` on the <xref:System.Net.Http.HttpRequestMessage>.
252+
253+
In the following file upload example:
254+
255+
* `content` is the file's <xref:System.Net.Http.HttpContent>.
256+
* `/Filesave` is the web API endpoint.
257+
* `Http` is the <xref:System.Net.Http.HttpClient>.
258+
259+
```csharp
260+
var request = new HttpRequestMessage(HttpMethod.Post, "/Filesave");
261+
request.SetBrowserRequestStreamingEnabled(true);
262+
request.Content = content;
263+
264+
var response = await Http.SendAsync(request);
265+
```
266+
267+
Streaming requests:
268+
269+
* Require HTTPS protocol and don't work on HTTP/1.x.
270+
* Include a body but not a `Content-Length` header. [CORS](xref:security/cors) is required, and CORS preflight request is always issued.
271+
272+
For more information on file uploads with an <xref:Microsoft.AspNetCore.Components.Forms.InputFile> component, see <xref:blazor/file-uploads#file-size-read-and-upload-limits> and the example at [Upload files to a server with client-side rendering (CSR)](xref:blazor/file-uploads#upload-files-to-a-server-with-client-side-rendering-csr).
273+
274+
:::moniker-end
275+
245276
## Add the `HttpClient` service
246277

247278
*The guidance in this section applies to client-side scenarios.*
@@ -257,10 +288,7 @@ In the `Program` file, add an <xref:System.Net.Http.HttpClient> service if it is
257288

258289
```csharp
259290
builder.Services.AddScoped(sp =>
260-
new HttpClient
261-
{
262-
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
263-
});
291+
new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
264292
```
265293

266294
The preceding example sets the base address with `builder.HostEnvironment.BaseAddress` (<xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment.BaseAddress%2A?displayProperty=nameWithType>), which gets the base address for the app and is typically derived from the `<base>` tag's `href` value in the host page.
@@ -274,10 +302,7 @@ If you're calling an external web API (not in the same URL space as the client a
274302

275303
```csharp
276304
builder.Services.AddScoped(sp =>
277-
new HttpClient
278-
{
279-
BaseAddress = new Uri("https://localhost:5001")
280-
});
305+
new HttpClient { BaseAddress = new Uri("https://localhost:5001") });
281306
```
282307

283308
## JSON helpers
@@ -524,7 +549,7 @@ If you're calling an external web API (not in the same URL space as the client a
524549

525550
```csharp
526551
builder.Services.AddHttpClient("WebAPI", client =>
527-
client.BaseAddress = new Uri(https://localhost:5001));
552+
client.BaseAddress = new Uri("https://localhost:5001"));
528553
```
529554

530555
In the following component code:
@@ -624,7 +649,7 @@ If you're calling an external web API (not in the same URL space as the client a
624649

625650
```csharp
626651
builder.Services.AddHttpClient<ForecastHttpClient>(client =>
627-
client.BaseAddress = new Uri(https://localhost:5001));
652+
client.BaseAddress = new Uri("https://localhost:5001"));
628653
```
629654

630655
Components inject the typed <xref:System.Net.Http.HttpClient> to call the web API.

0 commit comments

Comments
 (0)