diff --git a/aspnetcore/blazor/call-web-api.md b/aspnetcore/blazor/call-web-api.md index affb475cfb..501bd443fa 100644 --- a/aspnetcore/blazor/call-web-api.md +++ b/aspnetcore/blazor/call-web-api.md @@ -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 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 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 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("FrontendUrl")}/movies"; - - private HttpClient Http { get; set; } = httpClient; - public async Task GetMoviesAsync(bool watchedMovies) { - var requestUri = watchedMovies ? $"{serviceEndpoint}/watched" : - serviceEndpoint; - - return await Http.GetFromJsonAsync(requestUri) ?? []; + return await http.GetFromJsonAsync("movies") ?? []; } } ``` @@ -690,8 +685,6 @@ namespace BlazorSample.Client; public class ForecastHttpClient(HttpClient http) { - private readonly HttpClient http = http; - public async Task GetForecastAsync() { return await http.GetFromJsonAsync("forecast") ?? [];