我目前在新的
.NET 8 Blazor
项目中遇到路由问题。当尝试导航到 Razor 类库中定义的页面时,我遇到 404 错误。
Razor 类库已在 Blazor Web 应用程序中正确引用,并且
Routes.razor
文件已进行相应配置:
<Router AppAssembly="@typeof(Program).Assembly"
AdditionalAssemblies="new[]{
typeof(PostsPage).Assembly // Razor class library reference
}">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(Layout.MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
我在将现有的.NET 7项目升级到.NET 8后遇到了同样的问题。根据升级文档,从.NET 7过渡到.NET 8时不需要额外的配置。
有人知道这个问题以及如何解决它吗?
我通过调用 Program.cs 类中的“AddAdditionalAssemblies”方法解决了该问题:
app.MapRazorComponents<App>()
.AddAdditionalAssemblies(typeof(PostsPage).Assembly) // the solution
.AddInteractiveServerRenderMode();
app.Run();
欲了解更多信息,您可以看看这篇文章。