【问题标题】:How to support routes with and without language in URL in multilingual asp.net MVC website如何在多语言 asp.net MVC 网站的 URL 中支持有语言和无语言的路由
【发布时间】:2013-12-11 13:36:01
【问题描述】:

尝试为我当前的 mvc 项目设置本地化。 我需要配置路由以支持这两个 url:有和没有语言(例如 www.domain.com/en/about/contacts 和 www.domain.com/about/contact)。默认语言的 URL 中没有 lang 的路由。

我这样定义路线

            routes.MapRoute(
            name: "Localization",
            url: "{lang}/{controller}/{action}/{identifier}",
            defaults: new { lang = defaultLang, controller = "Home", action = "Index", identifier = UrlParameter.Optional }
        );

但我无法满足一项重要要求 - 我的默认语言路由在 URL 中应该是“无语言”,直到用户指定所需的语言。即使该语言 == defaultLanguage 我需要我的路线包含它(语言),因为它是由用户选择的。

【问题讨论】:

    标签: asp.net-mvc localization asp.net-mvc-routing


    【解决方案1】:

    解决了!

    @Localization@ ​​路由应如下所示:

    routes.MapRoute(
                name: "Localization",
                url: "{lang}/{controller}/{action}/{identifier}",
                defaults: new { controller = "Home", action = "Index", identifier = UrlParameter.Optional },
                constraints: new { lang = @"[a-z]{2}" }
            );
    
    • 不应为 lang 提供默认值
    • 我需要为 lang 添加约束,以帮助路由器识别带有和不带有 lang_s 的 URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-20
      • 2011-10-08
      • 1970-01-01
      • 2010-11-19
      • 2011-08-17
      • 1970-01-01
      • 2012-03-21
      • 2010-09-16
      相关资源
      最近更新 更多