ASP.NET Core 3.0 Razor页面中的路由本地化 [英] Routed localization in ASP.NET Core 3.0 Razor Pages

查看:259
本文介绍了ASP.NET Core 3.0 Razor页面中的路由本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET Core 3.0 Razor Pages应用程序中使用路由本地化.

I would like to use routed localization in my ASP.NET Core 3.0 Razor Pages application.

https://stackoverflow.com/a/52976625/107718 似乎有针对2.2的解决方案,既然已经重新设计了路由/端点,那么在3.0中还有更好的方法吗?

https://stackoverflow.com/a/52976625/107718 seems to have a solution for 2.2 but is there any better way of doing this in 3.0 now that it have routing/endpoints reworked?

推荐答案

ASP.NET Core 3.0引入了端点路由的新功能,通过该功能,我们可以在路由数据进入MVC之前从路由数据中获取区域性.这使我们能够根据当前路线来本地化内容,而无需付出太多努力.

ASP.NET Core 3.0 introduces a new feature of Endpoint Routing by which we can get the culture from the route data before it goes into MVC. This allows us to localize content according to current route without too much effort.

首先,请确保本地化服务已注册&您已按照以下方式配置了支持的区域性:

Firstly, make sure the Localization Service is registered & you've configured supported culture as below:

services.AddLocalization(opts =>  opts.ResourcesPath = "Resources" );
services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]{
        new CultureInfo("en-US"),
        new CultureInfo("de"),
        new CultureInfo("it"),
        // ... others
    };
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
    options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
});

然后添加UseRequestLocalization中间件并配置区域性路由,以便它可以正确获取区域性信息:

And then add an UseRequestLocalization Middleware and configure a route for culture such that it can get the culture info correctly:


app.UseRouting();
app.UseRequestLocalization();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(name: "culture-route", pattern:"{culture=en-US}/{controller=Home}/{action=Index}/{id?}"); 
    endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

请注意,上述订单很重要.

对不起,我没有注意到您要求提供Razor Page.要使用Razor Page WebApp,请创建自定义IPageRouteModelConvention来映射路线:

Sorry, I didn't notice that you asked for the Razor Page. To work with Razor Page WebApp, create a custom IPageRouteModelConvention to map the route:

public class CustomCultureRouteRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        List<SelectorModel> selectorModels = new List<SelectorModel>();
        foreach (var selector in model.Selectors.ToList())
        {
            var template = selector.AttributeRouteModel.Template;
            selectorModels.Add(new SelectorModel(){
                AttributeRouteModel = new AttributeRouteModel
                {
                    Template = "/{culture}" + "/" + template
                }
            });
        }
        foreach(var m in selectorModels){
            model.Selectors.Add(m);
        }
    }
}

并添加此页面约定:

services.AddRazorPages().AddRazorPagesOptions(opts =>
{
    opts.Conventions.Add(new CustomCultureRouteRouteModelConvention());
});
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]{
        new CultureInfo("en-US"),
        new CultureInfo("de"),
        new CultureInfo("it"),
        new CultureInfo("zh"),
    };
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
    options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
});

中间件:

app.UseRouting();

app.UseRequestLocalization();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    //endpoints.MapControllerRoute(name: "culture-route", pattern:"{culture=en-US}/{controller=Home}/{action=Index}/{id?}");
    //endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

这篇关于ASP.NET Core 3.0 Razor页面中的路由本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆