C# 如何在启用了ASP.NET Core 2.2 EndpointRouting的情况下使用RouteDataRequestCultureProvider?
我正在尝试在一个新的ASP.NET Core 2.2 MVC项目中使用C# 如何在启用了ASP.NET Core 2.2 EndpointRouting的情况下使用RouteDataRequestCultureProvider?,c#,asp.net-core,localization,asp.net-core-mvc,asp.net-core-routing,C#,Asp.net Core,Localization,Asp.net Core Mvc,Asp.net Core Routing,我正在尝试在一个新的ASP.NET Core 2.2 MVC项目中使用RouteDataRequestCultureProvider 我已经阅读了上的Microsoft文档以了解2.2中引入的更改,但我不明白为什么“文化”不被认为是URL生成的环境值 我在Startup.cs中更新了ConfigureServices,以包括以下设置: var supportedCultres = new[] { new CultureInfo("en"), new CultureInfo("fr") }; se
RouteDataRequestCultureProvider
我已经阅读了上的Microsoft文档以了解2.2中引入的更改,但我不明白为什么“文化”不被认为是URL生成的环境值
我在Startup.cs中更新了ConfigureServices
,以包括以下设置:
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 } };
});
当我按预期导航到/en或/fr时,此路由将解析为HomeController.Index()
,但指向锚定标记辅助对象的其他操作的任何链接都将呈现为
但我不明白为什么需要进行任何更改,因为“区域性”路由值已经存在于
RouteData.Values
集合中,并且由锚定标记帮助器在以前的路由模型中自动使用。这些都是有效的操作路由,那么当路由包含区域性时,为什么URL生成会失败呢?我对net core 3.1也有同样的问题。我终于找到了原因并找到了解决办法
TLDNR:由于核心2.2环境路由值不再传递到url生成器中,因此需要显式地传递它们。我通过扩展razor anchor标记帮助程序解决了这个问题。你解决了这个问题吗?我在core 3.1中遇到了同样的问题-
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?}");
});
services.AddMvc(opts => { opts.EnableEndpointRouting = false; })
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
<a asp-route-culture="en" asp-controller="Home" asp-action="About">About</a>