多语言网站:Azure部署的行为与localhost不同

编辑:完整的代码可以在github存储库中找到。

我已经在这篇文章 (1)和多语言网站的MVC4代码 (2)之后构建了一个ASP.NET MVC5多语言应用程序。

在(2)它使用“技巧”来解决“我希望在语言Y中呈现完整的X.cshtml”问题:它添加了一个后缀ViewName.fr.cshtml,以便视图自动重定向到正确的语言。 这是代码,我认为这是与我的问题相关的代码的唯一部分:

public class LocalizedViewEngine : RazorViewEngine { public override ViewEngineResult FindPartialView (ControllerContext controllerContext, string partialViewName, bool useCache) { List searched = new List(); if (!string.IsNullOrEmpty(partialViewName)) { ViewEngineResult result; result = base.FindPartialView(controllerContext, string.Format("{0}.{1}", partialViewName, CultureInfo.CurrentUICulture.Name), useCache); if (result.View != null) { return result; } searched.AddRange(result.SearchedLocations); result = base.FindPartialView(controllerContext, string.Format("{0}.{1}", partialViewName, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName), useCache); if (result.View != null) { return result; } searched.AddRange(result.SearchedLocations); } return new ViewEngineResult(searched.Distinct().ToList()); } public override ViewEngineResult FindView (ControllerContext controllerContext, string viewName, string masterName, bool useCache) { List searched = new List(); if (!string.IsNullOrEmpty(viewName)) { ViewEngineResult result; result = base.FindView(controllerContext, string.Format("{0}.{1}", viewName, CultureInfo.CurrentUICulture.Name), masterName, useCache); if (result.View != null) { return result; } searched.AddRange(result.SearchedLocations); result = base.FindView(controllerContext, string.Format("{0}.{1}", viewName, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName), masterName, useCache); if (result.View != null) { return result; } searched.AddRange(result.SearchedLocations); } return new ViewEngineResult(searched.Distinct().ToList()); } } 

问题是 :虽然web在localhost中工作得很好,但在将网站部署到Azure时似乎会出现行为变化(请参阅http://educa03.org )。

默认语言设置为加泰罗尼亚语(ca-ES)。 这很好。 您可以在右上角将语言更改为西class牙语(“ES”),这里是它背后的剃刀代码:

  

此语言更改适用于索引(主页) 之外的所有页面。 就像在这种情况下,它不是出于某种原因搜索index.es.chtml …再次:它在localhost中的行为不是这样,所以我不知道如何调试它。 此外,所有其他页面也可以在Azure上正常工作。

这是路由配置:

 // (some captcha-specific routing) routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // BotDetect requests must not be routed: routes.IgnoreRoute("{*botdetect}", new { botdetect = @"(.*)BotDetectCaptcha\.ashx" }); // the language route: routes.MapRoute( "Default", "{culture}/{controller}/{action}/{id}", new { culture = "ca", controller = "Home",//ControllerName action = "Index",//ActionName id = UrlParameter.Optional } ).RouteHandler = new LocalizedMvcRouteHandler(); 

所以这就是我解决问题的方法:

  public ActionResult Index() { // The Index is the only page that does not respond to the LocalizedViewEngine as it should... var currentCulture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; if (currentCulture != LocalizationAttribute.DefaultCulture) return View(string.Format("Index.{0}", currentCulture)); else return View(); } 

也就是说:我强迫唯一的情况(动作索引)没有被重新路由到{View}.{Culture}.cshtml ,并强制在控制器中的Action内部这样做。 我不喜欢这个解决方案,但至少它有效。 并且它仅在这种非常特殊的情况下本地化,这是由于Azure部署以某种方式处理索引与我的localhost IIS不同。