You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8 Blazor Web App(WASM)模板疑似bug:error页面跳转至404

.NET 8 Blazor WASM全局交互模式下错误页面自动跳404的问题

问题表现

  • 创建.NET 8 Blazor Web App(WebAssembly)项目时,选择GLOBAL交互位置(此为关键操作)
  • 生成项目结构并运行后,访问/error页面:初始显示错误页面,但1秒后自动跳转至404页面
  • 该问题仅在全局交互模式下出现,在Components/Pages下创建的自定义页面也会触发跳转至404的情况

解决办法

方法1:显式映射error路由

在Program.cs中添加手动路由映射,避免/error被404逻辑捕获:

builder.Services.AddRazorComponents()
    .AddInteractiveWebAssemblyComponents();

app.MapRazorComponents<App>()
    .AddInteractiveWebAssemblyRenderMode()
    .AddAdditionalAssemblies(typeof(Client._Imports).Assembly);

// 显式映射/error路由,跳过404拦截
app.Map("/error", () => Results.Content(
    "<!DOCTYPE html><html><head><title>Error</title></head><body><h1>An error occurred</h1></body></html>", 
    "text/html"));

方法2:调整Router组件的NotFound逻辑

在App.razor中修改NotFound区域代码,判断当前路由为/error时不触发404页面:

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        @if (!NavigationManager.Uri.EndsWith("/error"))
        {
            <LayoutView Layout="@typeof(MainLayout)">
                <p>抱歉,这个地址不存在。</p>
            </LayoutView>
        }
    </NotFound>
</Router>

原因分析

全局交互模式下,Blazor客户端路由初始化时会优先匹配客户端路由规则,但默认的/error页面未被显式加入可匹配路由列表,导致客户端路由系统误判其为不存在的页面,触发404跳转逻辑。

内容的提问来源于stack exchange,提问作者Aleksander Chelpski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:43:20