为特定 URL 定义路线

Define route for specific URL

我的 ASP.NET MVC 5 项目是多语言的,我使用 culture 来改变语言。

我已经定义了文化的默认路由如下:

public static void RegisterRoutes(RouteCollection routes)
{
    var _cul = CultureHelper.GetCurrentCulture() != null
            ? CultureHelper.GetCurrentCulture()
            : ConfigurationSettings.AppSettings["WebSiteDefaultCulture"];

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
            "Default",
            "{culture}/{Controller}/{action}/{id}",
            new {culture = _cul, controller = "Home", action = "Index", id = UrlParameter.Optional}
        );
    routes.MapRoute(
            "Home",
            "{culture}/Home/{action}/{id}",
            new {action = "Index", id = UrlParameter.Optional}
        );
}

所以本站所有页面的URL只会用culture打开。

我想打开一些没有文化的特定网站URL。

比如这个URLhttps://test.com/en-US/Cart/Cart/5不用提供文化也可以打开

谢谢。

解决方案是定义两个路由,有文化和无文化

routes.MapRoute(
            name: "DefaultWithCulture",
            url: "{culture}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { culture = _cul }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { culture = _cul, controller = "Home", action = "Index", id = UrlParameter.Optional }
            );