Blazor Server项目Fluent UI动态导入模块加载失败求助
问题:Fluent UI Blazor Server动态导入模块失败
运行Fluent UI Blazor Server项目时反复遇到动态导入模块失败的问题:服务器返回text/html MIME类型,而非JavaScript模块要求的类型,导致Fluent UI资源无法加载。
项目初始运行数周无异常,仅修改部分.razor文件后该问题频繁出现。尝试删除.vs、bin文件夹,清理重建解决方案后仅临时生效;换设备运行正常,但本地多次运行后问题复现。访问对应JS模块URL(https://localhost:7246/_content/Microsoft.FluentUI.AspNetCore.Components/Microsoft.FluentUI.AspNetCore.Components.lib.module.js)时,返回自定义404页面。
Program.cs代码
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.FluentUI.AspNetCore.Components; using TestIdentityScaffoldingFluentUi.Components; using TestIdentityScaffoldingFluentUi.Components.Account; using TestIdentityScaffoldingFluentUi.Data; using TestIdentityScaffoldingFluentUi.Services; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddFluentUIComponents(); builder.Services.AddDataGridEntityFrameworkAdapter(); builder.Services.AddCascadingAuthenticationState(); builder.Services.AddScoped<IdentityUserAccessor>(); builder.Services.AddScoped<IdentityRedirectManager>(); builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>(); builder.Services.AddAuthentication(options => { options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme; }) .AddIdentityCookies(); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(connectionString)); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>() .AddSignInManager() .AddDefaultTokenProviders(); builder.Services.Configure<IdentityOptions>(options => { options.User.RequireUniqueEmail = true; }); builder.Services.AddSingleton<IEmailSender<ApplicationUser>, SendGridEmailSender>(); builder.Services.Configure<SendGridEmailSenderOptions>(options => { options.ApiKey = builder.Configuration["SendGrid:ApiKey"]; options.SenderEmail = builder.Configuration["SendGrid:SenderEmail"]; options.SenderName = builder.Configuration["SendGrid:SenderName"]; }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseStatusCodePagesWithRedirects("/404"); app.UseAntiforgery(); app.MapRazorComponents<App>() .AddInteractiveServerRenderMode(); // Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints(); app.Run();
App.razor代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <base href="/" /> <link rel="stylesheet" href="app.css" /> <link rel="stylesheet" href="TestIdentityScaffoldingFluentUi.styles.css" /> <link rel="icon" type="image/x-icon" href="favicon.ico" /> <HeadOutlet @rendermode="RenderModeForPage" /> </head> <body> <Routes @rendermode="RenderModeForPage" /> <script src="_framework/blazor.web.js"></script> <script src="https://code.jquery.com/jquery-3.7.1.slim.js" integrity="sha256-UgvvN8vBkgO0luPSUl2s8TIlOSYRoGFAX4jlCIm9Adc=" crossorigin="anonymous"></script> </body> </html> @code { [CascadingParameter] private HttpContext HttpContext { get; set; } = default!; private IComponentRenderMode RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account") ? null : InteractiveServer; }
内容的提问来源于stack exchange,提问作者BigThinker
相关产品推荐
相关产品推荐

