Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多语言网站:Azure部署与localhost不同的行为_C#_Asp.net_Asp.net Mvc_Azure_Azure Web App Service - Fatal编程技术网

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

C# 多语言网站:Azure部署与localhost不同的行为,c#,asp.net,asp.net-mvc,azure,azure-web-app-service,C#,Asp.net,Asp.net Mvc,Azure,Azure Web App Service,编辑:完整代码可在中找到 我已经为多语言网站构建了一个ASP.NETMVC5多语言应用程序 在(2)中,它使用了一个“技巧”来解决“我希望完整的X.cshtml以语言Y呈现”问题:它添加了一个后缀ViewName.fr.cshtml,以便将视图自动重定向到正确的语言。这是代码,我认为这是代码中与我的问题相关的唯一部分: public class LocalizedViewEngine : RazorViewEngine { public override ViewEngineResult

编辑:完整代码可在中找到

我已经为多语言网站构建了一个ASP.NETMVC5多语言应用程序

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

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

我就是这样解决这个问题的:

    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
的案例(操作索引),并强制在控制器中的操作中这样做。我不喜欢这个解决方案,但至少它是有效的。它只在这种非常特殊的情况下进行了本地化,这是由于Azure部署对索引的处理方式与我的localhost IIS有所不同

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