由于我是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/admin而不是en-US/Admin,我还需要在用户首次打开网站时在URL中设置默认区域性,例如例如为siteName并加载默认区域性,甚至默认情况下加载siteName/zh-CN
also i want to exclude the admin folder from this convention to be siteName/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 and loads default culture, or even loads siteName/en-US by Default
谢谢.
推荐答案您可以使用 AddFolderRouteModelConvention .该文档具有示例有关操作方法的示例,我已针对您的目的对其进行了修改:
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页面中的默认路由
发布评论