Blazor 服务器外部链接路由到未找到

问题描述 投票:0回答:1

我的网站是 https://medbase.co.zw

如果我导航到应用程序内的页面,它就可以工作。但如果我将网址复制并粘贴到另一个选项卡中,它会路由到“找不到”。

https://medbase.co.zw/questions/13尝试单击此

这是在我将应用程序更改为 .NET 8、删除 _Host.cshtml、将应用程序重命名为路由并将应用程序变成现在的样子之后开始的。此外,我还必须添加

<HeadOutlet @rendermode="@RenderMode.InteractiveServer" />
<Routes @rendermode="@RenderMode.InteractiveServer" />
,以便该应用程序可以与 MudBlazor 配合使用。我发现如果删除渲染模式标签,MudBlazor 导航面板将无法工作。

相关代码粘贴在下面

App.razor

<!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="/" />
    <link href="MedbaseBlazor.styles.css" rel="stylesheet" />

    <link href="_content/MedbaseComponents/wwwroot/css/mdstyles.css" rel="stylesheet" />
    <link rel="stylesheet" href="/css/bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="_content/MedbaseComponents/wwwroot/css/common.css" />

    <HeadOutlet @rendermode="@RenderMode.InteractiveServer" />
    <link href="css/site.css" rel="stylesheet" />

    <!--WhatsApp Metas-->
    <meta property="og:site_name" content="Medbase">

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

    <link rel="apple-touch-icon" sizes="180x180" href="~/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="~/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="~/favicon-16x16.png">
    <link rel="manifest" href="~/site.webmanifest">
    <meta name="theme-color" content="#80b0fa">

    <!--Mudblazor References-->
    <!-- Google Tag Manager -->
    <!-- End Google Tag Manager -->

    <!--Microsoft Clarity-->

    <!--Google Ads Line-->

</head>
<body>
    <Routes @rendermode="@RenderMode.InteractiveServer" />

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>
</body>
</html>

路线.razor

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).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>
    </Router>
</CascadingAuthenticationState>

程序.cs

using MedbaseLibrary.Services;
using MudBlazor;
using MudBlazor.Services;
using MedbaseBlazor;
using System.IdentityModel.Tokens.Jwt;
using MedbaseBlazor.Pages;
using MedbaseLibrary.Essays;
using MedbaseLibrary.Notes;
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using MedbaseLibrary.Store;
using MedbaseLibrary.Questions;
using MedbaseLibrary.CoursesAndTopics;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

var environment = args.Contains("--local") ? Environments.Development : Environments.Production;

//Dependencies
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;


builder.Services.AddCascadingAuthenticationState();
builder.Services.AddMudServices();
builder.Services.AddMudMarkdownServices();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseRouting();

app.UseAntiforgery();

app.UseDeveloperExceptionPage();

app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
app.UseStatusCodePagesWithRedirects("/StatusCode/{0}");

app.Run();

blazor blazor-server-side mudblazor
1个回答
0
投票

问题在于,虽然 Blazor SPA 路由器通过在 Router 中注册附加程序集了解路由,但服务器却不知道。

您需要在程序中注册库:

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode()
    .AddAdditionalAssemblies(.... add all your assemblies here);
© www.soinside.com 2019 - 2024. All rights reserved.