Localized page names with ASP.NET Core 2.1

前端 未结 1 1876
孤独总比滥情好
孤独总比滥情好 2021-01-14 15:04

When create a Razor page, e.g. \"Events.cshtml\", one get its model name set to

@page
@model EventsModel

where the page\'s name in this cas

相关标签:
1条回答
  • 2021-01-14 15:40

    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; }
    }
    

    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
                        }
                    });
                }
            }
        }
    }
    

    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.

    You register the new convention in ConfigureServices:

    services.AddMvc().AddRazorPagesOptions(options =>
    {
        options.Conventions.Add(new LocalizedPageRouteModelConvention(new LocalizationService()));
    }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
    0 讨论(0)
提交回复
热议问题