gpt4 book ai didi

c# - 多语言网站 : Azure deployment different behavior than localhost

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:26 26 4
gpt4 key购买 nike

编辑:完整的代码可以在 github repository 中找到。 .

我已经按照 this post 构建了一个 ASP.NET MVC5 多语言应用程序(1) 和this MVC4 code (2) 对于多语言网站。

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

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

问题:虽然网络在本地主机中运行得很好,但将网站部署到 Azure 时,行为似乎发生了变化(请参阅 http://educa03.org )。

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

<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>

此语言更改适用于除索引(主页)之外的所有页面。就像在这种情况下,由于某种原因,它不会搜索 index.es.chtml ... 再说一次:它在本地主机中的行为不是这样的,所以我不知道如何调试它。另外,所有其他页面都可以正常工作,在 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 Index),并强制在 Action 内部这样做在 Controller 中。我不喜欢这个解决方案,但至少它有效。它仅在这种非常特殊的情况下进行本地化,这是由于 Azure 部署对索引的处理方式与我的本地主机 IIS 有所不同。

关于c# - 多语言网站 : Azure deployment different behavior than localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com