如何在ASP.NET Core Razor Pages中为单页应用程序创建包罗万象的路由? [英] How do I create a catch-all route for single page apps in ASP.NET Core Razor Pages?

查看:46
本文介绍了如何在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()添加路由,如

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中为单页应用程序创建包罗万象的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆