Asp.net-Mvc

屬性路由和本地化問題

  • March 4, 2014

我在 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 時,它只建構預設的英文版本,不會給我西班牙語版本。誰能告訴我為什麼會這樣?我不能為我的數字的生活解決這個問題。

您是否嘗試過更新 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 } );
 }
}

引用自:https://stackoverflow.com/questions/10344384