本文介绍了如何在ASP.NET Core Razor Pages中为单页应用程序创建包罗万象的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于单页应用程序,我们希望能够将所有其他未处理的请求路由到索引,以便处理客户端路由.
For a single page app, we want to be able to route all otherwise-unhandled requests to the index in order to handle routing client-side.
以前,我们将使用MapRoute()添加路由,如此答案,但是,当使用Razor页面作为索引时,这似乎不起作用.
Previously, we would add routes using MapRoute() as detailed in this answer, however, this doesn't seem to work when using a Razor Page as our index.
我们如何创建Razor Pages索引的后备?
How do we create a fallback to a Razor Pages index?
推荐答案为此,请将路线添加到剃刀页面"选项,如下所示:
In order to do this, add the route to the Razor Pages options like so:
services.AddMvc() .AddRazorPagesOptions(options => { // Match all routes to the index so we can handle routing client-side. options.Conventions.AddPageRoute("/index", "{*url}"); })然后确保像往常一样配置静态文件服务:
And then ensure that static file serving is configured as usual:
app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( "default", "{controller=Home}/{action=Index}/{id?}"); });更多推荐
如何在ASP.NET Core Razor Pages中为单页应用程序创建包罗万象的路由?
发布评论