更改ASP.NET Core Razor页面中的默认路由 [英] Changing default route in ASP.NET Core Razor Pages

查看:102
本文介绍了更改ASP.NET Core Razor页面中的默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是ASP.NET Core中Razor Pages概念的新手,我想应用一个通用URL来将区域性参数传递给路由

as i'm new to Razor Pages concept in ASP.NET Core, i want to apply a general URL to pass the culture parameter to the route

我已经使用MVC做到了这一点,但我也想将其应用于Razor页面这是我在MVC中所做的工作以及根据需要进行的工作

i have done that using MVC but i would like also to apply it with Razor pages here is what i have done in MVC and its working as needed

  routes.MapRoute(
                    name: "default",
                    template: "{culture}/{controller=Home}/{action=Index}/{id?}");

我已将其应用到特定的Page中并且也可以正常工作

i have applied it with specific Page and its working too

   options.Conventions.AddPageRoute("/RealEstate/Index", "{culture}/RealEstate");

但是当我想申请所有页面时,它不起作用,我也不知道应该以PageName的形式传递什么信息

but when i want to apply for all pages it doesn't work and i don't know what should be passed as a PageName

 options.Conventions.AddPageRoute("*", "{culture}/{*url}");

我也想从此约定中将admin文件夹排除为siteName.com/admin而不是en-US/Admin,我还需要在用户首次打开网站时在URL中设置默认区域性,例如例如为siteName.com并加载默认区域性,甚至默认情况下加载siteName.com/zh-CN

also i want to exclude the admin folder from this convention to be siteName.com/admin instead of en-US/Admin also i need to set a default culture in the URL when the user opens the site for first time, like for example to be siteName.com and loads default culture, or even loads siteName.com/en-US by Default

谢谢.

推荐答案

您可以使用示例有关操作方法的示例,我已针对您的目的对其进行了修改:

You can apply a route model convention to a folder using AddFolderRouteModelConvention. The docs have an example of how to do this, which I've taken and modified for your purposes:

options.Conventions.AddFolderRouteModelConvention("/", model =>
{
    foreach (var selector in model.Selectors)
    {
        selector.AttributeRouteModel = new AttributeRouteModel
        {
            Order = -1,
            Template = AttributeRouteModel.CombineTemplates(
                "{culture}",
                selector.AttributeRouteModel.Template),
        };
    }
});

假定将"/" 设置为文件夹,因此这对所有页面都采用约定,因此适用于根级别.而不是像我链接的示例中那样添加新的选择器,而是将现有选择器修改为在 {culture} 令牌之前添加,您可以按名称在页面中访问该令牌,例如:

This applies a convention to all pages, given that "/" is set as the folder and therefore applies at the root level. Rather than adding a new selector as in the example I linked, this modifies the existing selector to prepend the {culture} token, which you can access in your pages by name, e.g.:

public void OnGet(string culture)
{
    // ...
}

如果我们添加了一个选择器,则无论是否带有区域性都可以访问这些页面,从而使其成为可选项.按照我展示的方法,如操作说明中所示,需要 {culture} 令牌.

Had we added a new selector, the pages would be accessible both with and without the culture, making it optional. With the approach I've shown, the {culture} token is required, as indicated in the OP.

这篇关于更改ASP.NET Core Razor页面中的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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