在 Blazor Server 项目中,如果使用了含有许多路径层级的 URL,可能会出现资源加载失败的问题。此时可以通过在 Startup.cs 文件的 Configure 方法中添加以下代码,将请求路径最大长度提高到 2048 来解决该问题:
app.UsePathBase("/app"); // 将应用程序的基路径设置为“/app”(假定基路径为“http://localhost:5000/app”)
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.Length > 2048)
{
context.Response.StatusCode = 404;
return;
}
await next();
});
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToClientSideBlazor("index.html");
});
下一篇:Blazor长路径加载资源失败