我正在尝试RouteDataRequestCultureProvider在新的ASP.NET Core 2.2 MVC项目中使用。
我已经阅读了有关ASP.NET Core中路由的Microsoft文档,以了解2.2中引入的更改,但是我不明白为什么“文化”不被视为URL生成的环境值。
我ConfigureServices在Startup.cs中进行了更新,以包括以下设置:
var supportedCultres = new[] { new CultureInfo("en"), new CultureInfo("fr") };
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en");
options.SupportedCultures = supportedCultres;
options.SupportedUICultures = supportedCultres;
options.RequestCultureProviders = new[] { new RouteDataRequestCultureProvider { Options = options } };
});
Run Code Online (Sandbox Code Playgroud)
我修改了应用程序和默认路由Configure以使用“文化”路径段:
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(locOptions);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{culture:regex(^(en|fr)$)}/{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
HomeController.Index()当我按预期导航到/ en或/ fr时,此路线将解析为,但与锚定标记帮助器进行的其他操作的任何链接都将呈现为<a href="">(包括支架产生的“隐私”链接)。
关闭EnableEndpointRouting会导致锚标记助手再次工作:
services.AddMvc(opts => { opts.EnableEndpointRouting = false; …Run Code Online (Sandbox Code Playgroud) c# localization asp.net-core-mvc asp.net-core asp.net-core-routing