如何在ASP.NET Core Razor Pages中为单页应用程序创建包罗万象的路由?

编程入门 行业动态 更新时间:2024-10-09 13:23:33
本文介绍了如何在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中为单页应用程序创建包罗万象的路由?

本文发布于:2023-11-28 08:50:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:包罗万象   路由   中为   应用程序   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!