属性路由和本地化的问题

时间:2012-04-27 03:31:03

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

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

我有一个Route Prefix设置如下: [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时,它只构建默认的英文版本,并且不会给我西班牙语版本。谁能让我深入了解为什么会出现这种情况?我不能为了这个人的生活而努力。

1 个答案:

答案 0 :(得分:1)

您是否尝试过更新RouteValueDictionary并将其作为参数传递给您的网址助手?我做了类似的事情来切换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 } );
  }
}