Drop prop and field in examples (#31996)
parent
ee7c1dfc93
commit
e98de861ad
|
@ -34,7 +34,11 @@ Call an external (not in the Blazor Web App) todo list web API from a Blazor Web
|
|||
For client-side rendering (CSR), which includes Interactive WebAssembly components and Auto components that have adopted CSR, calls are made with a preconfigured <xref:System.Net.Http.HttpClient> registered in the `Program` file of the client project (`BlazorApp.Client`):
|
||||
|
||||
```csharp
|
||||
builder.Services.AddScoped(sp => new HttpClient { ... });
|
||||
builder.Services.AddScoped(sp =>
|
||||
new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(builder.Configuration["FrontendUrl"] ?? "https://localhost:5002")
|
||||
});
|
||||
```
|
||||
|
||||
For server-side rendering (SSR), which includes prerendered and interactive Server components, prerendered WebAssembly components, and Auto components that are prerendered or have adopted SSR, calls are made with an <xref:System.Net.Http.HttpClient> registered in the `Program` file of the server project (`BlazorApp`):
|
||||
|
@ -52,23 +56,14 @@ Call an internal (inside the Blazor Web App) movie list API, where the API resid
|
|||
|
||||
For CSR, which includes Interactive WebAssembly components and Auto components that have adopted CSR, calls to the API are made via a client-based service (`ClientMovieService`) that uses a preconfigured <xref:System.Net.Http.HttpClient> registered in the `Program` file of the client project (`BlazorApp.Client`). Because these calls are made over a public or private web, the movie list API is a *web API*.
|
||||
|
||||
The following example obtains a list of movies:
|
||||
The following example obtains a list of movies from the `/movies` endpoint:
|
||||
|
||||
```csharp
|
||||
public class ClientMovieService(IConfiguration config, HttpClient httpClient)
|
||||
: IMovieService
|
||||
public class ClientMovieService(HttpClient http) : IMovieService
|
||||
{
|
||||
private readonly string serviceEndpoint =
|
||||
$"{config.GetValue<string>("FrontendUrl")}/movies";
|
||||
|
||||
private HttpClient Http { get; set; } = httpClient;
|
||||
|
||||
public async Task<Movie[]> GetMoviesAsync(bool watchedMovies)
|
||||
{
|
||||
var requestUri = watchedMovies ? $"{serviceEndpoint}/watched" :
|
||||
serviceEndpoint;
|
||||
|
||||
return await Http.GetFromJsonAsync<Movie[]>(requestUri) ?? [];
|
||||
return await http.GetFromJsonAsync<Movie[]>("movies") ?? [];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -690,8 +685,6 @@ namespace BlazorSample.Client;
|
|||
|
||||
public class ForecastHttpClient(HttpClient http)
|
||||
{
|
||||
private readonly HttpClient http = http;
|
||||
|
||||
public async Task<Forecast[]> GetForecastAsync()
|
||||
{
|
||||
return await http.GetFromJsonAsync<Forecast[]>("forecast") ?? [];
|
||||
|
|
Loading…
Reference in New Issue