使用ASP.NET Core 2.1的本地化页面名称

编程入门 行业动态 更新时间:2024-10-24 19:13:25
本文介绍了使用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

example/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

example/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的本地化页面名称

本文发布于:2023-11-15 06:36:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   页面   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!