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
+119-2Lines changed: 119 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,6 +93,123 @@ Calls a todo list web API from a Blazor WebAssembly app:
93
93
94
94
:::moniker-end
95
95
96
+
## Client-side scenarios for calling external web APIs
97
+
98
+
Client-based components call external web APIs using <xref:System.Net.Http.HttpClient> instances, typically created with a preconfigured <xref:System.Net.Http.HttpClient> registered in the `Program` file:
The following Razor component makes a request to a web API for GitHub branches similar to the *Basic Usage* example in the <xref:fundamentals/http-requests> article.
109
+
110
+
`CallWebAPI.razor`:
111
+
112
+
```razor
113
+
@page "/call-web-api"
114
+
@using System.Text.Json
115
+
@using System.Text.Json.Serialization
116
+
@inject HttpClient Client
117
+
118
+
<h1>Call web API from a Blazor WebAssembly Razor component</h1>
119
+
120
+
@if (getBranchesError || branches is null)
121
+
{
122
+
<p>Unable to get branches from GitHub. Please try again later.</p>
using var responseStream = await response.Content.ReadAsStreamAsync();
153
+
branches = await JsonSerializer.DeserializeAsync
154
+
<IEnumerable<GitHubBranch>>(responseStream);
155
+
}
156
+
else
157
+
{
158
+
getBranchesError = true;
159
+
}
160
+
161
+
shouldRender = true;
162
+
}
163
+
164
+
public class GitHubBranch
165
+
{
166
+
[JsonPropertyName("name")]
167
+
public string? Name { get; set; }
168
+
}
169
+
}
170
+
```
171
+
172
+
In the preceding example for C# 12 or later, an empty array (`[]`) is created for the `branches` variable. For earlier versions of C# compiled with an SDK earlier than .NET 8, create an empty array (`Array.Empty<GitHubBranch>()`).
173
+
174
+
<!-- A version of the following content is also in the
175
+
Security > WebAssembly > Overview article under
176
+
the heading: "Web API requests" -->
177
+
178
+
To protect .NET/C# code and data, use [ASP.NET Core Data Protection](xref:security/data-protection/introduction) features with a server-side ASP.NET Core backend web API. The client-side Blazor WebAssembly app calls the server-side web API for secure app features and data processing. For more information, see <xref:blazor/call-web-api?pivots=webassembly> and the articles and examples in this documentation node.
179
+
180
+
Blazor WebAssembly apps are often prevented from making direct calls across origins to web APIs due to [Cross-Origin Request Sharing (CORS) security](xref:blazor/call-web-api#cross-origin-resource-sharing-cors). A typical exception looks like the following:
181
+
182
+
> :::no-loc text="Access to fetch at '{URL}' from origin 'https://localhost:{PORT}' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.":::
183
+
184
+
Even if you call <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestMode%2A> with a <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.BrowserRequestMode> field of `NoCors` (1) seeking to circumvent the preceding exception, the request often fails due to CORS restrictions on the web API's origin, such as a restriction that requires CORS or a restriction that prevents JavaScript [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch) requests from a browser. The only way for such calls to succeed is for the web API that you're calling to allow your origin to call its origin with the correct CORS configuration. Most external web APIs don't allow you to configure their CORS policies. To deal with this restriction, adopt either of the following strategies:
185
+
186
+
* Maintain your own server-side ASP.NET Core backend web API. The client-side Blazor WebAssembly app calls your server-side web API, and your web API makes the request from its server-based C# code (not a browser) to the external web API with the correct CORS headers, returning the result to your client-side Blazor WebAssembly app.
187
+
188
+
* Use a proxy service to proxy the request from the client-side Blazor WebAssembly app to the external web API. The proxy service uses a server-side app to make the request on the client's behalf and returns the result after the call succeeds. In the following example based on [CloudFlare's CORS PROXY](https://corsproxy.io/), the `{REQUEST URI}` placeholder is the request URI:
189
+
190
+
```razor
191
+
@using System.Net
192
+
@inject IHttpClientFactory ClientFactory
193
+
194
+
...
195
+
196
+
@code {
197
+
public async Task CallApi()
198
+
{
199
+
var client = ClientFactory.CreateClient();
200
+
201
+
var urlEncodedRequestUri = WebUtility.UrlEncode("{REQUEST URI}");
202
+
203
+
var request = new HttpRequestMessage(HttpMethod.Get,
204
+
$"https://corsproxy.io/?{urlEncodedRequestUri}");
205
+
206
+
var response = await client.SendAsync(request);
207
+
208
+
...
209
+
}
210
+
}
211
+
```
212
+
96
213
## Server-side scenarios for calling external web APIs
97
214
98
215
Server-based components call external web APIs using <xref:System.Net.Http.HttpClient> instances, typically created using <xref:System.Net.Http.IHttpClientFactory>. For guidance that applies to server-side apps, see <xref:fundamentals/http-requests>.
@@ -115,7 +232,7 @@ The following Razor component makes a request to a web API for GitHub branches s
115
232
@using System.Text.Json.Serialization
116
233
@inject IHttpClientFactory ClientFactory
117
234
118
-
<h1>Call web API from a Blazor Server Razor component</h1>
235
+
<h1>Call web API from a server-side Razor component</h1>
119
236
120
237
@if (getBranchesError || branches is null)
121
238
{
@@ -171,7 +288,7 @@ else
171
288
}
172
289
```
173
290
174
-
In the preceding example for C# 12 or later, an empty array (`[]`) is created for the `branches` variable. For earlier versions of C#, create an empty array (`Array.Empty<GitHubBranch>()`).
291
+
In the preceding example for C# 12 or later, an empty array (`[]`) is created for the `branches` variable. For earlier versions of C# compiled with an SDK earlier than .NET 8, create an empty array (`Array.Empty<GitHubBranch>()`).
175
292
176
293
For an additional working example, see the server-side file upload example that uploads files to a web API controller in the <xref:blazor/file-uploads#upload-files-to-a-server-with-server-side-rendering> article.
Copy file name to clipboardExpand all lines: aspnetcore/blazor/security/webassembly/index.md
+14-8Lines changed: 14 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,10 @@ For examples of the preceding approaches, see <xref:blazor/security/webassembly/
29
29
30
30
## Web API requests
31
31
32
+
<!-- A version of this content is also in the Call web API
33
+
article under the heading:
34
+
"Client-side scenarios for calling external web APIs" -->
35
+
32
36
To protect .NET/C# code and data, use [ASP.NET Core Data Protection](xref:security/data-protection/introduction) features with a server-side ASP.NET Core backend web API. The client-side Blazor WebAssembly app calls the server-side web API for secure app features and data processing. For more information, see <xref:blazor/call-web-api?pivots=webassembly> and the articles and examples in this documentation node.
33
37
34
38
Blazor WebAssembly apps are often prevented from making direct calls across origins to web APIs due to [Cross-Origin Request Sharing (CORS) security](xref:blazor/call-web-api#cross-origin-resource-sharing-cors). A typical exception looks like the following:
@@ -38,6 +42,7 @@ Blazor WebAssembly apps are often prevented from making direct calls across orig
38
42
Even if you call <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestMode%2A> with a <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.BrowserRequestMode> field of `NoCors` (1) seeking to circumvent the preceding exception, the request often fails due to CORS restrictions on the web API's origin, such as a restriction that requires CORS or a restriction that prevents JavaScript [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch) requests from a browser. The only way for such calls to succeed is for the web API that you're calling to allow your origin to call its origin with the correct CORS configuration. Most external web APIs don't allow you to configure their CORS policies. To deal with this restriction, adopt either of the following strategies:
39
43
40
44
* Maintain your own server-side ASP.NET Core backend web API. The client-side Blazor WebAssembly app calls your server-side web API, and your web API makes the request from its server-based C# code (not a browser) to the external web API with the correct CORS headers, returning the result to your client-side Blazor WebAssembly app.
45
+
41
46
* Use a proxy service to proxy the request from the client-side Blazor WebAssembly app to the external web API. The proxy service uses a server-side app to make the request on the client's behalf and returns the result after the call succeeds. In the following example based on [CloudFlare's CORS PROXY](https://corsproxy.io/), the `{REQUEST URI}` placeholder is the request URI:
42
47
43
48
```razor
@@ -47,18 +52,19 @@ Even if you call <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssem
47
52
...
48
53
49
54
@code {
50
-
...
55
+
public async Task CallApi()
56
+
{
57
+
var client = ClientFactory.CreateClient();
51
58
52
-
var client = ClientFactory.CreateClient();
59
+
var urlEncodedRequestUri = WebUtility.UrlEncode("{REQUEST URI}");
53
60
54
-
var urlEncodedRequestUri = WebUtility.UrlEncode("{REQUEST URI}");
55
-
56
-
var request = new HttpRequestMessage(HttpMethod.Get,
57
-
$"https://corsproxy.io/?{urlEncodedRequestUri}");
61
+
var request = new HttpRequestMessage(HttpMethod.Get,
0 commit comments