Netcore 2.2 本地化路由 - 默认文化的路由值始终被忽略

Netcore 2.2 Localized Routing - Route values for default culture always ignored

成功使用 Localized routing using ASP.NET Core MVC 2 中布置的项目。该项目的开发目的是仅使用 controller/Action 和替代语言的本地化路由来显示默认语言。

我的控制器

[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...下面的代码为法语和西班牙语提供了一个完全本地化的路由,但默认路由始终只是 controller/action。如果我手动输入完全本地化的英文路线,页面会加载,但下面的代码总是 returns 只是一个常规路径。

var culture = CultureInfo.CurrentCulture.Name;

var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", culture).Url;
//should be "en/portal/Dashboard/Dashboardv1"
//currently resolves to "Dashboard/Dashboardver1"

建议为 ?

我发现在 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;

它returns/en/Portal/Dashboard/Dashboardver1