Multilingual website: Azure deployment different behavior than localhost

。_饼干妹妹 提交于 2019-12-07 21:53:04

问题


Edit: the full code can be found in the github repository.

I have build a ASP.NET MVC5 multilingual application following this post (1) and this MVC4 code (2) for multilingual websites.

On (2) it uses a "trick" to solve the "I want the full X.cshtml rendered in language Y" problem: it adds a suffix ViewName.fr.cshtml so the view is automatically redirected to the correct language. This is the code for that, which I think is the only part of the code relevant to my problem:

public class LocalizedViewEngine : RazorViewEngine
{
    public override ViewEngineResult FindPartialView (ControllerContext controllerContext, string partialViewName, bool useCache)
    {
        List<string> searched = new List<string>();

        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<string> searched = new List<string>();

        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());
    }
}

The problem: although the web works perfectly fine in localhost, there seems to be a change of behavior when deploying the website to Azure (see http://educa03.org ).

The default language is set to Catalan (ca-ES). That works fine. You can change the language to Spanish ("ES") on the top-right corner, and here is the razor code behind it:

<ul class="nav navbar-nav navbar-right">
    <li>@Html.ActionLink("es", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { culture = "es" }, null)</li>
    <li>@Html.ActionLink("cat", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { culture = "ca-ES" }, null)</li>
</ul>

This language change works fine for all pages except for index (the home page). Like in this case it is not searching for index.es.chtml for some reason... Again: it does not behave like this in localhost, so I don't know how to debug it. Plus all the other pages work fine, also on Azure.

Here is the route config:

// (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();

回答1:


So this is how I solved the issue:

    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();
    }

That is to say: I forced the only case (Action Index) that was not being re-routed to {View}.{Culture}.cshtml, and forced to do so inside the Action in the controller. I don't like the solution, but at least it works. And it is only localized in this very special case that is due to the Azure deployment that treats Index somehow differently than my localhost IIS does.



来源:https://stackoverflow.com/questions/35068513/multilingual-website-azure-deployment-different-behavior-than-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!