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

Blazor Server外部链接复制粘贴跳转至NotFound问题求助

问题描述

应用内部页面跳转正常,但直接在新标签页打开深层URL(如/questions/13)时,会跳转到NotFound页面。该问题出现在升级至.NET 8之后:移除了_Host.cshtml,将原App组件重命名为Routes并调整了应用结构;为适配MudBlazor,添加了<HeadOutlet @rendermode="@RenderMode.InteractiveServer" />和<Routes @rendermode="@RenderMode.InteractiveServer" />,移除这些渲染模式标签会导致MudBlazor导航面板失效。

解决方案

这个问题的核心是服务器端未正确处理初始深层路径请求,以下是具体修复步骤:

1. 修正Program.cs中的路由配置

在MapRazorComponents<App>()配置中添加回退路由,确保所有非静态文件请求都能被Blazor路由器处理:

var app = builder.Build();

// ... 其他中间件配置(HttpsRedirection、StaticFiles等) ...

app.UseAntiforgery();

// 配置Blazor组件路由并添加交互式服务器渲染模式
app.MapRazorComponents<App>()
   .AddInteractiveServerRenderMode()
   // 注册包含页面组件的额外程序集,让路由器能找到这些页面
   .AddAdditionalAssemblies(
       typeof(MedbaseComponents.Pages.Questions).Assembly,
       typeof(MedbaseComponents.Pages.Topics).Assembly,
       typeof(MedbaseComponents.Pages.Store.Store).Assembly,
       typeof(MedbaseComponents.Pages.Essays.Essays).Assembly,
       typeof(MedbaseComponents.Pages.Articles.Articles).Assembly,
       typeof(MedbaseComponents.Pages.Notes.Notes).Assembly,
       typeof(MedbaseComponents.Pages.Questions2.QuestionsPage2).Assembly
   );

// 添加回退路由:所有非静态文件的请求都转发给App组件处理
app.MapFallbackToComponent<App>("{*path:nonfile}");

// ... 其他配置(StatusCodePages等) ...

app.Run();

注意:如果你的项目中还保留了_Host.cshtml,则使用app.MapFallbackToPage("/_Host")替代MapFallbackToComponent。

2. 优化Routes.razor的Router配置

将AppAssembly指向当前项目的程序集,避免路由解析时找不到正确的组件:

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly"
            AdditionalAssemblies="new[]
            {
                typeof(MedbaseComponents.Pages.Questions).Assembly, 
                typeof(MedbaseComponents.Pages.Topics).Assembly, 
                typeof(MedbaseComponents.Pages.Store.Store).Assembly,
                typeof(MedbaseComponents.Pages.Essays.Essays).Assembly,
                typeof(MedbaseComponents.Pages.Articles.Articles).Assembly,
                typeof(MedbaseComponents.Pages.Notes.Notes).Assembly,
                typeof(MedbaseComponents.Pages.Questions2.QuestionsPage2).Assembly,
            }">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(WebLayout)" />
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
        <!-- 添加NotFound模板,明确路由未找到的情况 -->
        <NotFound>
            <LayoutView Layout="@typeof(WebLayout)">
                <p>抱歉,未找到请求的页面。</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

3. 确认App.razor的基础配置

确保base href="/"正确设置,这是Blazor路由解析的核心基础,同时保留交互式服务器渲染模式以兼容MudBlazor:

<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" /> <!-- 必须正确设置 -->
    <!-- 其余样式、Meta标签等内容不变 -->
    <HeadOutlet @rendermode="@RenderMode.InteractiveServer" />
</head>
<body>
    <Routes @rendermode="@RenderMode.InteractiveServer" />
    <!-- 错误UI等内容不变 -->
</body>
</html>

问题根源说明

在.NET 8的Blazor Interactive Server模式下,当用户直接访问深层URL时,服务器默认会尝试寻找对应物理文件或API端点,而非转发给Blazor路由器。添加回退路由后,所有非静态文件的请求都会被导向Blazor的App组件,由客户端路由器正确解析路径,从而解决直接打开URL时的404问题。同时,注册额外程序集能确保路由器发现类库中的页面组件,避免路由匹配失败。

内容的提问来源于stack exchange,提问作者Tanaka Mawere

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 21:35:10