0
votes

Trying to understand the default Blazor WebAssembly ASP.NET Hosted template ... my question is exactly what's the location of the API hosted by the server app?

The client app simply calls an API "weatherforecast":

await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");

But there is no such URL if you navigate manually to http://localhost:xxxxx/weatherforecast

What's the actual API endpoint if I want to see the raw JSON result?

1

1 Answers

0
votes

In you blazor component, a HttpClient is injected.

@* Injection of HttpClient *@
@inject HttpClient Http
@* your page/component code *@
@foreach (var f in forecasts)
{
    <div>
        @f.SomeProperty
    </div>
}

@code {
    private WeatherForecast[] forecasts;

    protected override Task OnInitilizeAsync()
    {
        // Use HttpClient injected at top
        forecasts = Http.GetFromJsonAsync<WeatherForecast[]>("weatherforecast");
    }
}

In you Program class, you have a Main(string[]) method, which register all services:

    // create a Builder for Blazor WebAssembly
    var builder = WebAssemblyHostBuilder.CreateDefault(args);

    // Register a HttpClient in services
    // builder.HostEnvironment.BaseAddress = domain of your app (localhost, www.example.com, ...)
    builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });