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
> The code examples in this article adopt [nullable reference types (NRTs) and .NET compiler null-state static analysis](xref:migration/50-to-60#nullable-reference-types-nrts-and-net-compiler-null-state-static-analysis), which are supported in ASP.NET Core 6.0 or later. When targeting ASP.NET Core 5.0 or earlier, remove the null type designation (`?`) from the `string?`, `TodoItem[]?`, `WeatherForecast[]?`, and `IEnumerable<GitHubBranch>?` types in the article's examples.
20
22
21
23
:::zone pivot="webassembly"
22
24
23
25
> [!NOTE]
24
-
> This article has loaded **Blazor WebAssembly** coverage for calling web APIs. The [Blazor Server coverage](?pivots=server) addresses the following subjects:
26
+
> This article has loaded **WebAssembly**render mode coverage for calling web APIs. The [Server render mode coverage](?pivots=server) addresses the following subjects:
25
27
>
26
28
> * Use of the `HttpClient` factory infrastructure to provide an `HttpClient` to the app.
27
-
> * Cross-Origin Resource Sharing (CORS) pertaining to Blazor Server apps.
29
+
> * Cross-Origin Resource Sharing (CORS) pertaining to server-side components.
28
30
> * Blazor framework component examples for testing web API access.
29
31
> * Additional resources for developing Blazor Server apps that call a web API.
30
32
31
-
[Blazor WebAssembly](xref:blazor/hosting-models#blazor-webassembly) apps call web APIs using a preconfigured <xref:System.Net.Http.HttpClient> service, which is focused on making requests back to the server of origin. Additional <xref:System.Net.Http.HttpClient> service configurations for other web APIs can be created in developer code. Requests are composed using Blazor JSON helpers or with <xref:System.Net.Http.HttpRequestMessage>. Requests can include [Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API) option configuration.
33
+
Client-side components call web APIs using a preconfigured <xref:System.Net.Http.HttpClient> service, which is focused on making requests back to the server of origin. Additional <xref:System.Net.Http.HttpClient> service configurations for other web APIs can be created in developer code. Requests are composed using Blazor JSON helpers or with <xref:System.Net.Http.HttpRequestMessage>. Requests can include [Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API) option configuration.
32
34
33
35
## Examples in this article
34
36
@@ -51,7 +53,25 @@ public class TodoItem
51
53
52
54
For guidance on how to create a server-side web API, see <xref:tutorials/first-web-api>. For information on Cross-Origin Resource Sharing (CORS), see the *Cross-Origin Resource Sharing (CORS)* section later in this article.
53
55
54
-
The Blazor WebAssembly examples that demonstrate obtaining weather data from a server API are based on a hosted Blazor WebAssembly solution created from the [Blazor WebAssembly project template](xref:blazor/project-structure#blazor-webassembly).
56
+
The Blazor examples that demonstrate obtaining weather data from a server API are based on a hosted Blazor WebAssembly solution created from the [Blazor WebAssembly project template](xref:blazor/project-structure#blazor-webassembly).
57
+
58
+
:::moniker range=">= aspnetcore-8.0"
59
+
60
+
<!-- UPDATE 8.0 Cross-link SSR and CSR -->
61
+
62
+
For server-side components in Blazor Web Apps that require server interactivity, add the server-side rendering (SSR) attribute to the top of the component:
63
+
64
+
```razor
65
+
@attribute [RenderModeServer]
66
+
```
67
+
68
+
For client-side components in Blazor Web Apps that require client interactivity, add the client-side rendering (CSR) attribute to the top of the component:
69
+
70
+
```razor
71
+
@attribute [RenderModeWebAssembly]
72
+
```
73
+
74
+
:::moniker-end
55
75
56
76
## Package
57
77
@@ -61,7 +81,7 @@ The [`System.Net.Http.Json`](https://www.nuget.org/packages/System.Net.Http.Json
61
81
62
82
## Add the `HttpClient` service
63
83
64
-
In `Program.cs`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:
84
+
In the `Program` file, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:
65
85
66
86
```csharp
67
87
builder.Services.AddScoped(sp=>
@@ -359,7 +379,7 @@ public static class JSONPatchInputFormatter
359
379
360
380
Configure the web API's controllers to use the `Microsoft.AspNetCore.Mvc.NewtonsoftJson` package and process PATCH requests with the JSON PATCH input formatter. Insert the `JSONPatchInputFormatter` in the first position of MVC's input formatter collection so that it processes requests prior to any other input formatter.
361
381
362
-
In `Program.cs` modify the call to <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers%2A>:
382
+
In the `Program` file modify the call to <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers%2A>:
363
383
364
384
```csharp
365
385
builder.Services.AddControllers(options=>
@@ -449,7 +469,7 @@ Add the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/Microsoft.E
449
469
450
470
[!INCLUDE[](~/includes/package-reference.md)]
451
471
452
-
In `Program.cs`:
472
+
In the `Program` file:
453
473
454
474
```csharp
455
475
builder.Services.AddHttpClient("WebAPI", client=>
@@ -464,7 +484,7 @@ In the following component code:
464
484
> [!NOTE]
465
485
> When targeting ASP.NET Core 5.0 or earlier, add `@using` directives to the following component for <xref:System.Net.Http?displayProperty=fullName>, <xref:System.Net.Http.Json?displayProperty=fullName>, and <xref:System.Threading.Tasks?displayProperty=fullName>.
466
486
467
-
`Pages/FetchDataViaFactory.razor`:
487
+
`FetchDataViaFactory.razor`:
468
488
469
489
```razor
470
490
@page "/fetch-data-via-factory"
@@ -543,7 +563,7 @@ public class WeatherForecastHttpClient
@@ -560,7 +580,7 @@ In the following component code:
560
580
> [!NOTE]
561
581
> When targeting ASP.NET Core 5.0 or earlier, add an `@using` directive to the following component for <xref:System.Threading.Tasks?displayProperty=fullName>.
562
582
563
-
`Pages/FetchDataViaTypedHttpClient.razor`:
583
+
`FetchDataViaTypedHttpClient.razor`:
564
584
565
585
```razor
566
586
@page "/fetch-data-via-typed-httpclient"
@@ -606,11 +626,11 @@ else
606
626
> [!NOTE]
607
627
> When targeting ASP.NET Core 5.0 or earlier, add `@using` directives to the following component for <xref:System.Net.Http?displayProperty=fullName> and <xref:System.Net.Http.Json?displayProperty=fullName>.
Blazor WebAssembly's implementation of <xref:System.Net.Http.HttpClient> uses [Fetch API](https://developer.mozilla.org/docs/Web/API/fetch). Fetch API allows the configuration of several [request-specific options](https://developer.mozilla.org/docs/Web/API/fetch#Parameters). Options can be configured with <xref:System.Net.Http.HttpRequestMessage> extension methods shown in the following table.
633
+
Blazor's client-side implementation of <xref:System.Net.Http.HttpClient> uses [Fetch API](https://developer.mozilla.org/docs/Web/API/fetch). Fetch API allows the configuration of several [request-specific options](https://developer.mozilla.org/docs/Web/API/fetch#Parameters). Options can be configured with <xref:System.Net.Http.HttpRequestMessage> extension methods shown in the following table.
614
634
615
635
| Extension method | Fetch API request property |
616
636
| --- | --- |
@@ -635,7 +655,7 @@ For more information on Fetch API options, see [MDN web docs: WindowOrWorkerGlob
635
655
636
656
The following example calls a web API. The example requires a running web API based on the sample app described by the <xref:tutorials/first-web-api> article. This example makes requests to the web API at `https://localhost:10000/api/TodoItems`. If a different web API address is used, update the `ServiceEndpoint` constant value in the component's `@code` block.
637
657
638
-
The following example makes a [Cross-Origin Resource Sharing (CORS)](xref:security/cors) request from `http://localhost:5000` or `https://localhost:5001` to the web API. Add the following CORS Middleware configuration to the web API's service's `Program.cs` file:
658
+
The following example makes a [Cross-Origin Resource Sharing (CORS)](xref:security/cors) request from `http://localhost:5000` or `https://localhost:5001` to the web API. Add the following CORS Middleware configuration to the web API's service's `Program` file:
639
659
640
660
```csharp
641
661
app.UseCors(policy=>
@@ -648,7 +668,7 @@ Adjust the domains and ports of `WithOrigins` as needed for the Blazor app. For
648
668
649
669
By default, ASP.NET Core apps use ports 5000 (HTTP) and 5001 (HTTPS). To run both apps on the same machine at the same time for testing, use a different port for the web API app (for example, port 10000). For more information on setting the port, see <xref:fundamentals/servers/kestrel/endpoints>.
@@ -665,7 +685,7 @@ In <xref:Microsoft.AspNetCore.Components.ComponentBase.OnInitializedAsync%2A> on
665
685
> [!NOTE]
666
686
> When targeting ASP.NET Core 5.0 or earlier, add `@using` directives to the following component for <xref:System.Net.Http?displayProperty=fullName>, <xref:System.Net.Http.Json?displayProperty=fullName>, and <xref:System.Threading.Tasks?displayProperty=fullName>.
667
687
668
-
`Pages/ReturnHTMLOnException.razor`:
688
+
`ReturnHTMLOnException.razor`:
669
689
670
690
```razor
671
691
@page "/return-html-on-exception"
@@ -728,33 +748,33 @@ For more information, see <xref:blazor/fundamentals/handle-errors>.
728
748
:::zone pivot="server"
729
749
730
750
> [!NOTE]
731
-
> This article has loaded **Blazor Server** coverage for calling web APIs. The [Blazor WebAssembly coverage](?pivots=webassembly) addresses the following subjects:
751
+
> This article has loaded **Server**render mode coverage for calling web APIs. The [WebAssembly render mode coverage](?pivots=webassembly) addresses the following subjects:
732
752
>
733
-
> *Blazor WebAssembly examples based on an client-side WebAssembly app that calls a web API to create, read, update, and delete todo list items.
753
+
> *Client-side examples that call a web API to create, read, update, and delete todo list items.
734
754
> *`System.Net.Http.Json` package.
735
755
> *`HttpClient` service configuration.
736
756
> *`HttpClient` and JSON helpers (`GetFromJsonAsync`, `PostAsJsonAsync`, `PutAsJsonAsync`, `DeleteAsync`).
737
757
> *`IHttpClientFactory` services and the configuration of a named `HttpClient`.
738
758
> * Typed `HttpClient`.
739
759
> *`HttpClient` and `HttpRequestMessage` to customize requests.
740
-
> * Call web API example with Cross-Origin Resource Sharing (CORS) and how CORS pertains to Blazor WebAssembly apps.
760
+
> * Call web API example with Cross-Origin Resource Sharing (CORS) and how CORS pertains to client-side components.
741
761
> * How to handle web API response errors in developer code.
742
762
> * Blazor framework component examples for testing web API access.
743
-
> * Additional resources for developing Blazor WebAssembly apps that call a web API.
763
+
> * Additional resources for developing client-side components that call a web API.
744
764
745
-
[Blazor Server](xref:blazor/hosting-models#blazor-server) apps call web APIs using <xref:System.Net.Http.HttpClient> instances, typically created using <xref:System.Net.Http.IHttpClientFactory>. For guidance that applies to Blazor Server, see <xref:fundamentals/http-requests>.
765
+
Server-based components call 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>.
746
766
747
-
A Blazor Server app doesn't include an <xref:System.Net.Http.HttpClient> service by default. Provide an <xref:System.Net.Http.HttpClient> to the app using the [`HttpClient` factory infrastructure](xref:fundamentals/http-requests).
767
+
A server-side app doesn't include an <xref:System.Net.Http.HttpClient> service by default. Provide an <xref:System.Net.Http.HttpClient> to the app using the [`HttpClient` factory infrastructure](xref:fundamentals/http-requests).
748
768
749
-
In `Program.cs`:
769
+
In the `Program` file:
750
770
751
771
```csharp
752
772
builder.Services.AddHttpClient();
753
773
```
754
774
755
-
The following Blazor Server 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.
775
+
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.
756
776
757
-
`Pages/CallWebAPI.razor`:
777
+
`CallWebAPI.razor`:
758
778
759
779
```razor
760
780
@page "/call-web-api"
@@ -828,9 +848,9 @@ Browser security restricts a webpage from making requests to a different domain
828
848
829
849
:::zone pivot="webassembly"
830
850
831
-
For information on CORS requests in Blazor WebAssembly apps, see <xref:blazor/security/webassembly/additional-scenarios#cross-origin-resource-sharing-cors>.
851
+
For information on client-side CORS requests, see <xref:blazor/security/webassembly/additional-scenarios#cross-origin-resource-sharing-cors>.
832
852
833
-
For information on CORS, see <xref:security/cors>. The article's examples don't pertain directly to Blazor WebAssembly apps, but the article is useful for learning general CORS concepts.
853
+
For information on CORS, see <xref:security/cors>. The article's examples don't pertain directly to Razor component scenarios, but the article is useful for learning general CORS concepts.
834
854
835
855
:::zone-end
836
856
@@ -878,7 +898,7 @@ Various network tools are publicly available for testing web API backend apps di
878
898
:::zone pivot="webassembly"
879
899
880
900
*<xref:blazor/security/webassembly/additional-scenarios>: Includes coverage on using <xref:System.Net.Http.HttpClient> to make secure web API requests.
881
-
*<xref:security/cors>: Although the content applies to ASP.NET Core apps, not Blazor WebAssembly apps, the article covers general CORS concepts.
901
+
*<xref:security/cors>: Although the content applies to ASP.NET Core apps, not Razor components, the article covers general CORS concepts.
882
902
*[Cross-Origin Resource Sharing (CORS) at W3C](https://www.w3.org/TR/cors/)
Copy file name to clipboardExpand all lines: aspnetcore/blazor/forms-and-input-components.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2010,15 +2010,15 @@ If the server API returns the preceding default JSON response, it's possible for
2010
2010
}
2011
2011
```
2012
2012
2013
-
To modify the server API's response to make it only return the validation errors, change the delegate that's invoked on actions that are annotated with <xref:Microsoft.AspNetCore.Mvc.ApiControllerAttribute> in `Program.cs`. For the API endpoint (`/StarshipValidation`), return a <xref:Microsoft.AspNetCore.Mvc.BadRequestObjectResult> with the <xref:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary>. For any other API endpoints, preserve the default behavior by returning the object result with a new <xref:Microsoft.AspNetCore.Mvc.ValidationProblemDetails>.
2013
+
To modify the server API's response to make it only return the validation errors, change the delegate that's invoked on actions that are annotated with <xref:Microsoft.AspNetCore.Mvc.ApiControllerAttribute> in the `Program` file. For the API endpoint (`/StarshipValidation`), return a <xref:Microsoft.AspNetCore.Mvc.BadRequestObjectResult> with the <xref:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary>. For any other API endpoints, preserve the default behavior by returning the object result with a new <xref:Microsoft.AspNetCore.Mvc.ValidationProblemDetails>.
2014
2014
2015
-
Add the <xref:Microsoft.AspNetCore.Mvc?displayProperty=fullName> namespace to the top of the `Program.cs` file in the **:::no-loc text="Server":::** app:
2015
+
Add the <xref:Microsoft.AspNetCore.Mvc?displayProperty=fullName> namespace to the top of the `Program` file in the **:::no-loc text="Server":::** app:
2016
2016
2017
2017
```csharp
2018
2018
usingMicrosoft.AspNetCore.Mvc;
2019
2019
```
2020
2020
2021
-
In `Program.cs`, locate the <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViews%2A> extension method and add the following call to <xref:Microsoft.Extensions.DependencyInjection.MvcCoreMvcBuilderExtensions.ConfigureApiBehaviorOptions%2A>:
2021
+
In the `Program` file, locate the <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViews%2A> extension method and add the following call to <xref:Microsoft.Extensions.DependencyInjection.MvcCoreMvcBuilderExtensions.ConfigureApiBehaviorOptions%2A>:
2022
2022
2023
2023
```csharp
2024
2024
builder.Services.AddControllersWithViews()
@@ -2777,7 +2777,7 @@ public class SaladChef
2777
2777
}
2778
2778
```
2779
2779
2780
-
Register `SaladChef` in the app's DI container in `Program.cs`:
2780
+
Register `SaladChef` in the app's DI container in the `Program` file:
0 commit comments