使用ASP.NET Core 2.1的本地化页面名称 [英] Localized page names with ASP.NET Core 2.1
问题描述
在创建剃刀"页面时,例如"Events.cshtml",将其模型名称设置为
When create a Razor page, e.g. "Events.cshtml", one get its model name set to
@page
@model EventsModel
在这种情况下,页面的名称是事件",URL看起来像
where the page's name in this case is "Events", and the URL would look like
http://example.com/Events
为了能够使用挪威语中的页面名称,我将以下内容添加到"Startup.cs"中
To be able to use page name's in Norwegian I added the following to the "Startup.cs"
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorPagesOptions(options => {
options.Conventions.AddPageRoute("/Events", "/hvaskjer");
options.Conventions.AddPageRoute("/Companies", "/bedrifter");
options.Conventions.AddPageRoute("/Contact", "/kontakt");
});
与此同时,我也可以使用这样的网址,并继续投放事件"页面
With this I can also use an URL like this and still serve the "Events" page
http://example.com/hvaskjer
我打算支持更多的语言,并且想知道,这是设置本地化页面名称/路由的推荐方法吗?还是有一种更正确的正确方法来实现.
I'm planning to support many more languages and wonder, is this the recommended way to setup localized page name's/route's?, or is there a more proper, correct way to accomplish the same.
我的意思是,对于上面的示例,用10种语言显示15页,使用options.Conventions.AddPageRoute("/Page", "/side");
150次,它会感到混乱.
I mean, with the above sample, and having 15 pages in 10 languages it gets/feels messy using options.Conventions.AddPageRoute("/Page", "/side");
150 times.
推荐答案
您可以使用IPageRouteModelConvention
界面执行此操作.它提供了对PageRouteModel
的访问权限,您可以在其中有效地添加更多模板以用于路由以与特定页面进行匹配.
You can do this with the IPageRouteModelConvention
interface. It provides access to the PageRouteModel
where you can effectively add more templates for routes to match against for a particular page.
这是基于以下服务和模型的非常简单的概念证明:
Here's a very simple proof of concept based on the following service and model:
public interface ILocalizationService
{
List<LocalRoute> LocalRoutes();
}
public class LocalizationService : ILocalizationService
{
public List<LocalRoute> LocalRoutes()
{
var routes = new List<LocalRoute>
{
new LocalRoute{Page = "/Pages/Contact.cshtml", Versions = new List<string>{"kontakt", "contacto", "contatto" } }
};
return routes;
}
}
public class LocalRoute
{
public string Page { get; set; }
public List<string> Versions { get; set; }
}
它所做的只是提供特定页面的选项列表. IPageRouteModelConvention
实现如下所示:
All it does is provide the list of options for a particular page. The IPageRouteModelConvention
implementation looks like this:
public class LocalizedPageRouteModelConvention : IPageRouteModelConvention
{
private ILocalizationService _localizationService;
public LocalizedPageRouteModelConvention(ILocalizationService localizationService)
{
_localizationService = localizationService;
}
public void Apply(PageRouteModel model)
{
var route = _localizationService.LocalRoutes().FirstOrDefault(p => p.Page == model.RelativePath);
if (route != null)
{
foreach (var option in route.Versions)
{
model.Selectors.Add(new SelectorModel()
{
AttributeRouteModel = new AttributeRouteModel
{
Template = option
}
});
}
}
}
}
在启动时,Razor页面会构建应用程序的路由. Apply
方法针对框架找到的每个可导航页面执行.如果当前页面的相对路径与您的数据中的相对路径匹配,则会为每个选项添加一个附加模板.
At Startup, Razor Pages build the routes for the application. The Apply
method is executed for every navigable page that the framework finds. If the relative path of the current page matches one in your data, an additional template is added for each option.
您在ConfigureServices
中注册新约定:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.Add(new LocalizedPageRouteModelConvention(new LocalizationService()));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
这篇关于使用ASP.NET Core 2.1的本地化页面名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!