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
+20-17Lines changed: 20 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Learn how to call a web API from Blazor apps.
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: wpickett
7
7
ms.custom: mvc
8
-
ms.date: 04/29/2025
8
+
ms.date: 05/30/2025
9
9
uid: blazor/call-web-api
10
10
---
11
11
# Call a web API from ASP.NET Core Blazor
@@ -33,7 +33,7 @@ For more information, see the following resources:
33
33
34
34
## Microsoft identity platform for web API calls
35
35
36
-
Blazor Web Apps that use use [Microsoft identity platform](/entra/identity-platform/)/[Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra) can make streamlined web API calls with API provided by the [`Microsoft.Identity.Web.DownstreamApi` NuGet package](https://www.nuget.org/packages/Microsoft.Identity.Web.DownstreamApi).
36
+
Blazor Web Apps that use use [Microsoft identity platform](/entra/identity-platform/) with [Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra) can make streamlined web API calls with API provided by the [`Microsoft.Identity.Web.DownstreamApi` NuGet package](https://www.nuget.org/packages/Microsoft.Identity.Web.DownstreamApi).
37
37
38
38
[!INCLUDE[](~/includes/package-reference.md)]
39
39
@@ -291,13 +291,13 @@ The solution includes a demonstration of obtaining weather data securely via an
291
291
292
292
### `BlazorWebAppEntra`
293
293
294
-
ABlazorWebAppwithglobalAutointeractivitythatuses [Microsoftidentityplatform](/entra/identity-platform/)/[MicrosoftIdentityWebpackages](/entra/msal/dotnet/microsoft-identity-web/) for [MicrosoftEntraID](https://www.microsoft.com/security/business/microsoft-entra). The solution includes a demonstration of obtaining weather data securely via an external web API when a component that adopts Interactive Auto rendering is rendered on the client.
294
+
ABlazorWebAppwithglobalAutointeractivitythatuses [Microsoftidentityplatform](/entra/identity-platform/)with[MicrosoftIdentityWebpackages](/entra/msal/dotnet/microsoft-identity-web/) for [MicrosoftEntraID](https://www.microsoft.com/security/business/microsoft-entra). The solution includes a demonstration of obtaining weather data securely via an external web API when a component that adopts Interactive Auto rendering is rendered on the client.
295
295
296
296
### `BlazorWebAppEntraBff`
297
297
298
298
ABlazorWebAppwithglobalAutointeractivitythatuses:
299
299
300
-
* [Microsoftidentityplatform](/entra/identity-platform/)/[MicrosoftIdentityWebpackages](/entra/msal/dotnet/microsoft-identity-web/) for [MicrosoftEntraID](https://www.microsoft.com/security/business/microsoft-entra).
300
+
* [Microsoftidentityplatform](/entra/identity-platform/)with[MicrosoftIdentityWebpackages](/entra/msal/dotnet/microsoft-identity-web/) for [MicrosoftEntraID](https://www.microsoft.com/security/business/microsoft-entra).
ThesolutionincludesademonstrationofobtainingweatherdatasecurelyviaanexternalwebAPI when a component that adopts Interactive Auto rendering is rendered on the client.
@@ -1095,6 +1095,9 @@ public class CookieHandler : DelegatingHandler
FormoreinformationonFetchAPIoptions, see [MDNwebdocs: WindowOrWorkerGlobalScope.fetch():Parameters](https://developer.mozilla.org/docs/Web/API/fetch#Parameters).
Copy file name to clipboardExpand all lines: aspnetcore/blazor/components/httpcontext.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,19 @@ For additional context in *advanced* edge cases†, see the discussion in t
32
32
33
33
†Most developers building and maintaining Blazor apps don't need to delve into advanced concepts when the general guidance in this article is followed. The most important concept to keep in mind is that <xref:Microsoft.AspNetCore.Http.HttpContext> is fundamentally a server-based, request-response feature that's only generally available on the server during static SSR and only created when a user's circuit is established.
34
34
35
+
## Don't set or modify headers after the response starts
36
+
37
+
Attempting to set or modify a header after the first rendering (after the response starts) results in an error:
38
+
39
+
> :::no-loc text="System.InvalidOperationException: 'Headers are read-only, response has already started.'":::
40
+
41
+
Examples of situations that result in this error include:
42
+
43
+
* Calling <xref:Microsoft.AspNetCore.Identity.SignInManager%601.PasswordSignInAsync%2A?displayProperty=nameWithType>, which must set headers for Identity to function correctly, while adopting [streaming rendering](xref:blazor/components/rendering#streaming-rendering).
44
+
* Attempting to set or modify a header after the response has started during interactive rendering.
45
+
46
+
For guidance on setting headers before the response starts, see <xref:blazor/fundamentals/startup#control-headers-in-c-code>.
Copy file name to clipboardExpand all lines: aspnetcore/blazor/fundamentals/dependency-injection.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -610,6 +610,12 @@ public static class CircuitServicesServiceCollectionExtensions
610
610
}
611
611
```
612
612
613
+
Call `AddCircuitServicesAccessor` in the app's `Program` file:
614
+
615
+
```csharp
616
+
builder.Services.AddCircuitServicesAccessor();
617
+
```
618
+
613
619
Access the circuit-scoped services by injecting the `CircuitServicesAccessor` where it's needed.
614
620
615
621
For an example that shows how to access the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> from a <xref:System.Net.Http.DelegatingHandler> set up using <xref:System.Net.Http.IHttpClientFactory>, see <xref:blazor/security/additional-scenarios#access-authenticationstateprovider-in-outgoing-request-middleware>.
Copy file name to clipboardExpand all lines: aspnetcore/blazor/fundamentals/startup.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -525,6 +525,9 @@ Control headers at startup in C# code using the following approaches.
525
525
526
526
In the following examples, a [Content Security Policy (CSP)](https://developer.mozilla.org/docs/Web/HTTP/Guides/CSP) is applied to the app via a CSP header. The `{POLICY STRING}` placeholder is the CSP policy string. For more information on CSPs, see <xref:blazor/security/content-security-policy>.
527
527
528
+
> [!NOTE]
529
+
> Headers can't be set after the response starts. The approaches in this section only set headers before the response starts, so the approaches described here are safe. For more information, see <xref:blazor/components/httpcontext#dont-set-or-modify-headers-after-the-response-starts>.
530
+
528
531
### Server-side and prerendered client-side scenarios
529
532
530
533
Use [ASP.NET Core Middleware](xref:fundamentals/middleware/index) to control the headers collection.
0 commit comments