成功使用Localized routing using ASP.NET Core MVC 2上列出的项目。开发该项目以仅使用备用语言的控制器/操作和本地化路线来显示默认语言。
我的控制器
[LocalizationRoute("en", "Portal/Dashboard")]
[LocalizationRoute("fr", "Portail/tableau-de-bord")]
[LocalizationRoute("es", "Portal/Tablero")]
public class DashboardController : PortalBaseController
{
private const string Title = "Dashboard";
[LocalizationRoute("en", "Dashboardv1")]
[LocalizationRoute("fr", "tableau-de-bordv1")]
[LocalizationRoute("es", "Tablerov1")]
public IActionResult Dashboardver1()
在我的SignIn控制器中,我希望创建一个本地化的RedirectUrl ...下面的代码为法语和西班牙语提供了一个完全本地化的路由,但是默认路由始终只是控制器/操作。如果我手动输入完全本地化的英文路由,则会加载页面,但是下面的代码始终只返回常规路径。
var culture = CultureInfo.CurrentCulture.Name;
var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", culture).Url;
//should be "en/portal/Dashboard/Dashboardv1"
//currently resolves to "Dashboard/Dashboardver1"
建议为?
答案 0 :(得分:1)
我发现在LocalizationRouteDataHandler.AddControllerRouteData
中,代码ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
首先在控制器名称字典中添加了Culture =“ en”和route =“ Dashboard”,然后在尝试添加条件culture =“ en”和route =“ en / Portal / Dashboard”,由于添加了“ en”键,因此失败。
一种解决方法是,您可以删除不必要的密钥并添加新密钥。
LocalizationRouteDataHandler.cs
public static void AddControllerRouteData(string controller, string culture, string route)
{
if (controller == "Dashboard")
{
Console.WriteLine("test");
}
string controllerKey = controller.ToLower();
// If the controller doesn't exist, create it!
if (!ControllerRoutes.ContainsKey(controllerKey))
{
ControllerRoutes.TryAdd(controllerKey, new CultureControllerRouteData());
}
// key removed
if (culture=="en" && ControllerRoutes[controllerKey].Names.Remove("en"))
{
ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
}
// dictionary doesn't contain the key
else
{
ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
}
}
当我用
测试时var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", "en").Url;
它返回/en/Portal/Dashboard/Dashboardver1