我面临 .NET 8 和 Blazor 的问题。交互式渲染模式设置为自动(服务器和 WebAssembly)。问题是当我添加
NotAuthorized
和 NotFound
时,它无法导航到我的登录页面或我的 NotFound 页面。添加 UseStatusCodePagesWithRedirects
允许我导航到其中一个页面,但我希望未经授权的请求重定向到登录页面,并将不存在页面的请求重定向到 NotFound。
如果页面不存在,则会显示默认浏览器错误“找不到页面”。如果该页面确实存在并且我未登录,它将导航到“帐户/登录”而不是“/登录”,这是我的登录组件的路径。
您对如何实现这一目标有什么想法吗?我已经在互联网上搜索过,但找不到解决我的问题的完整解决方案。
@using HTPB.Client.Pages
@using Microsoft.AspNetCore.Components.Authorization
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)">
<NotAuthorized>
<Login></Login>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<NotFound></NotFound>
</NotFound>
</Router>
</CascadingAuthenticationState>
我也遇到同样的问题。也许这是微软的一个错误。该模板有效,因为它们在帐户/登录中有登录页面,但标记 NotAuthorized 未按 Routes.razor 中的预期工作