12

我在 Nuget 上使用最新版本的 AttributeRouting 包来为我的 ASP.Net MVC 项目设置路由。我正在创建一个有两种语言的网站,英语(主要)和西班牙语(次要)。两种语言的网址不同。例如,英语的 about us 是这样的:www.root.com/en/about-us,而西班牙语版本可能是这样的:www.root.com/es/sobre-nosotros。

我有一个路由前缀设置如下: [RoutePrefix("en", TranslationKey = "Home")]

然后,我创建了一个程序,将 XML 文件中的值读取到 FluentTranslationProvider 中。注册我的路线的代码如下所示:

var translations = new FluentTranslationProvider();
        translations
            .AddTranslations()
            .FromFile();

routes.MapAttributeRoutes(
            config =>
                {
                    config.AddRoutesFromControllersOfType<BaseController>();
                    config.AddTranslationProvider(translations);
                    config.CurrentUICultureResolver =
                        (httpContext, routeData) =>
                        (string) routeData.DataTokens["cultureName"] ??
                        Thread.CurrentThread.CurrentUICulture.Name;
                });

它似乎正在工作,因为我可以访问我的 Routes.axd 页面并查看以下内容:http: //imm.io/nm7Z

稍后在我的页面中,我的代码显示我的 CurrentCulture 设置为 es-AR,但是当我调用 URLHelper 类构建 url 时,它只构建默认的英文版本,不会给我西班牙版本。谁能告诉我为什么会这样?我不能为我的数字的生活解决这个问题。

4

1 回答 1

1

您是否尝试过更新 RouteValueDictionary 并将其作为参数传递给您的 url 助手?我为切换 ssl 做了类似的事情。

这里有一些示例代码需要考虑,作为辅助函数:

@functions {

  public static string LanguageUrl(WebViewPage page, string actionName, string controllerName, string desiredCulture)
  {
    // translate action name here, if needed.
    return page.Url.Action(actionName, controllerName, new { cultureName = desireCulture } );
  }
}
于 2014-03-04T16:44:02.770 回答