问题可能是出现在此处,因为Blazor WebAssembly不能访问本地网络资源。需要确保在Program.cs中注册了Http客户端,并允许非安全请求。
例如:
builder.Services.AddScoped(sp => new HttpClient {
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) })
.AddHttpMessageHandler();
// For server-side Blazor, authentication is configured in Startup.cs.
builder.Services.AddOidcAuthentication(options =>
{
// Configure your authentication provider options here.
// For more information, see https://aka.ms/blazor-standalone-auth
builder.Configuration.Bind("Local", options.ProviderOptions);
});
基于需要,您还可以添加其他配置选项。这是保证 HttpClient能够在 Blazor WebAssembly中执行调用所必需的步骤。
参考:https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-authentication-library?view=aspnetcore-5.0&tabs=visual-studio