正在尝试理解默认的Blazor WebAssembly ASP.NET托管模板。。。我的问题是服务器应用程序托管的API到底在什么位置?
客户端应用程序简单地调用API"API";天气预报":
await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
但是,如果您手动导航到http://localhost:xxxxx/weatherforecast
如果我想查看原始JSON结果,那么实际的API端点是什么?
在blazor组件中,注入了一个HttpClient。
@* 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");
}
}
在Program类中,有一个Main(string[])
方法,用于注册所有服务:
// 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) });