由于我对 ASP.NET Core 中的 Razor Pages 概念不熟悉,我想应用通用 URL 将区域性参数传递给路由
我已经使用 MVC 做到了这一点,但我也想将其应用于 Razor 页面
这是我在 MVC 中所做的以及它根据需要工作
routes.MapRoute(
name: "default",
template: "{culture}/{controller=Home}/{action=Index}/{id?}");
我已将其应用于特定页面并且它也起作用
options.Conventions.AddPageRoute("/RealEstate/Index", "{culture}/RealEstate");
但是当我想申请所有页面时,它不起作用,而且我不知道应该作为 PageName 传递什么
options.Conventions.AddPageRoute("*", "{culture}/{*url}");
我还想从该约定中排除 admin 文件夹,将其设置为 siteName.com/admin 而不是 en-US/Admin 我还需要在用户第一次打开网站时在 URL 中设置默认区域性,例如为 siteName.com 并加载默认区域性,甚至默认加载 siteName.com/en-US
Thanks.
您可以使用以下方式将路线模型约定应用于文件夹AddFolderRouteModelConvention https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.applicationmodels.pageconventioncollection.addfolderroutemodelconvention?view=aspnetcore-2.1。该文档有一个example https://learn.microsoft.com/en-us/aspnet/core/mvc/razor-pages/razor-pages-conventions?view=aspnetcore-2.1#page-route-action-conventions如何执行此操作,我已根据您的目的对其进行了修改:
options.Conventions.AddFolderRouteModelConvention("/", model =>
{
foreach (var selector in model.Selectors)
{
selector.AttributeRouteModel = new AttributeRouteModel
{
Order = -1,
Template = AttributeRouteModel.CombineTemplates(
"{culture}",
selector.AttributeRouteModel.Template),
};
}
});
这将约定应用于所有页面,因为"/"
设置为文件夹,因此适用于根级别。这不是像我链接的示例中那样添加新的选择器,而是修改现有的选择器以在前面添加{culture}
令牌,您可以通过名称在页面中访问该令牌,例如:
public void OnGet(string culture)
{
// ...
}
Had we added a new选择器,无论有没有区域性,页面都可以访问,使其成为可选的。通过我所展示的方法,{culture}
如 OP 中所示,需要令牌。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)