多语言asp.net mvc站点和路由

时间:2011-10-19 19:28:07

标签: asp.net-mvc-3 routing razor

我开发了多语言的asp.net mvc网站。语言存储在url中。默认值为英语。 我有BaseController类

protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string culture = filterContext.RouteData.Values[MvcApplication.CultureParamerName] as string;
            if (string.IsNullOrWhiteSpace(culture))
                culture = Facade.Common.GetLocale(BECulture.Cultures.English);//==en-us
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            base.OnActionExecuting(filterContext);
        }

和一些路线

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Photos_Route",
            "{culture}/photos/{action}/{id}",
            new { controller = "photos", action = "show", culture = "en-us" }
            );
        routes.MapRoute(
            "PhotosDownload_Route",
            "{culture}/photos/{action}/{id}/resolution/{measure1}x{measure2}",
            new { controller = "photos", action = "download", culture = "en-us" }
            );

        routes.MapRoute(
            "Pages_Router",
            "{culture}/page/{id}",
            new { controller = "home", action = "page", id = UrlParameter.Optional, culture = "en-us" }
            );

        routes.MapRoute(
            "Tags_Route", // Route name
            "{culture}/tag/{tag}", // URL with parameters
            new { controller = "photos", action = "ShowPhotosByTag", culture = "en-us" } // Parameter defaults
            );



routes.MapRoute(
            "Home_Route",
            "{culture}/{action}",
            new { controller = "home", culture = "en-us" }
            );

        routes.MapRoute(
            "Default_Route",
            "{culture}/{controller}/{action}/{id}",
            new { controller = "home", action = "index", id = 1, culture = "en-us" }
            );

    }

我想做以下事项:如果当前语言是英语,则url不得包含“en-us”值,因为英语是默认语言。反之亦然:如果url中没有参数"culture"(即RouteData.Values["culture"]中),则默认语言为英语。

我该怎么做?

更新:澄清我想要的内容。母版页(_Layout.cshtml)包含指向页面"About site"

的链接
 @Html.ActionLink(Resources.Strings.About_LinkText, "about", "home")

如果该网站使用英文指向/en-us/about。但我想(如果网站是英文的话)它应该指向/about(没有en-us)。

1 个答案:

答案 0 :(得分:1)

此路线设置的问题是确定路线的第一个标记是否为有效的文化名称。例如,您将不得不处理以下路线:

  • /fr-FR/home/index
  • /home/index

在第一种情况culture="fr-FR"和第二种情况culture="home"中。因此,为了解决这个问题,您可以尝试以下方式:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string culture = filterContext.RouteData.Values["culture"] as string;
    var ci = CultureInfo
        .GetCultures(CultureTypes.AllCultures)
        .FirstOrDefault(x => x.Name == culture);
    if (ci == null)
    {
        // we couldn't find a suitable culture => fallback to default
        ci = new CultureInfo("en-US");
    }

    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;
    base.OnActionExecuting(filterContext);
}