可能由于缓存问题,导致无法正确运行Blazor PWA应用程序。您可以尝试清除浏览器缓存并重新加载页面。以下是一个示例:
//在Program.cs中添加以下代码以配置服务Worker
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add("app");
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddAuthorizationCore();
builder.Services.AddSingleton();
builder.Services.AddSingleton();
builder.Services.AddScoped();
builder.Services.AddOidcAuthentication(options =>
{
options.ProviderOptions.Authority = "https://localhost:5000";
options.ProviderOptions.ClientId = "blazor-pwa";
options.ProviderOptions.RedirectUri = $"{builder.HostEnvironment.BaseAddress}authentication/login-callback";
options.ProviderOptions.ResponseType = "code";
options.ProviderOptions.PostLogoutRedirectUri = $"{builder.HostEnvironment.BaseAddress}authentication/logout-callback";
options.ProviderOptions.Scopes.Add("openid");
options.ProviderOptions.Scopes.Add("profile");
options.ProviderOptions.Scopes.Add("email");
options.ProviderOptions.Scopes.Add("api1");
options.UserOptions.NameClaim = "name";
options.UserOptions.RoleClaim = "role";
options.AccessTokenProvider = new AuthenticationAccessTokenProvider();
}).AddAccountClaimsPrincipalFactory();
builder.Services.AddHttpClient("API", client => client.BaseAddress = new Uri("https://localhost:5010"))
.AddHttpMessageHandler();
builder.Services.AddTransient(sp => sp.GetRequiredService().CreateClient("API"));
builder.Services.AddApiAuthorization();
await builder.Build().RunAsync();
}
请注意,此示例中使用了Blazored.LocalStorage和IdentityServer4。如果您不使用这些库,您可以将它们从代码中删除。
此外,您还可以尝试在Visual Studio命令提示符中使用“dotnet dev-certs https --