且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改ASP.NET Core Razor页面中的默认路由

更新时间:2021-09-15 08:39:46

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

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.