gpt4 book ai didi

c# - ASP.NET Core 3.0 Razor Pages 中的路由本地化

转载 作者:行者123 更新时间:2023-12-03 23:01:13 24 4
gpt4 key购买 nike

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

https://stackoverflow.com/a/52976625/107718似乎有 2.2 的解决方案,但是现在 3.0 有什么更好的方法可以做到这一点,因为它已经重新设计了路由/端点?

最佳答案

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

如何

首先,确保本地化服务已注册并且您已配置支持的文化,如下所示:

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中间件并为文化配置路由,以便它可以正确获取文化信息:
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?}");});

Note the above orders are important.

Demo:

enter image description here


[Edit]

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

关于c# - ASP.NET Core 3.0 Razor Pages 中的路由本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com