C# Asp.Net MVC:Html.ActionLink正在向我的URL添加wierd文本
我最近使用Plesk将我的asp.net mvc部署到基于Windows的主机上 本地一切正常,但部署应用程序后,我注意到我所有的链接都是字符串“aa”,而不是语言代码:C# Asp.Net MVC:Html.ActionLink正在向我的URL添加wierd文本,c#,asp.net-mvc,C#,Asp.net Mvc,我最近使用Plesk将我的asp.net mvc部署到基于Windows的主机上 本地一切正常,但部署应用程序后,我注意到我所有的链接都是字符串“aa”,而不是语言代码: mydomain.com/aa/Account/Register 而不是 mydomain.com/en/Account/Register 我怀疑这与我创建了Html.ActionLink的扩展有关,如下所示: private static string GetLocalizedController(string
mydomain.com/aa/Account/Register
而不是
mydomain.com/en/Account/Register
我怀疑这与我创建了Html.ActionLink的扩展有关,如下所示:
private static string GetLocalizedController(string controllerName, CultureInfo cultureInfo)
{
if (cultureInfo == null) cultureInfo = CultureInfo.CurrentCulture;
// arrange a "localized" controllerName to be handled with a dedicated localization-aware route.
string localizedControllerName = String.Format("{0}/{1}",
cultureInfo.TwoLetterISOLanguageName, controllerName);
return localizedControllerName;
}
public static IHtmlString ActionLink( this HtmlHelper helper, string linkText, string actionName,
string controllerName, object routeValues, string htmlAttributes, CultureInfo cultureInfo)
{
string localizedControllerName = GetLocalizedController(controllerName, cultureInfo);
return helper.ActionLink(linkText, actionName, localizedControllerName, routeValues, htmlAttributes);
}
这是我使用它的方式:
@Html.ActionLink("LinkText", "Index", "Home", null, null, (CultureInfo)null)
这就是我如何将语言放入url的方式:
public class LocalizedControllerActivator : IControllerActivator
{
public IController Create(RequestContext requestContext, Type controllerType)
{
//Get the {language} parameter in the RouteData
string lang = requestContext.RouteData.Values["lang"].ToString();
if (lang != ConfigurationManager.AppSettings["DefaultLanguage"])
{
try
{
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
}
catch (Exception e)
{
throw new NotSupportedException(String.Format("ERROR: Invalid language code '{0}'.", lang));
}
}
return DependencyResolver.Current.GetService(controllerType) as IController;
}
}
但是在localhost中,它可以工作,我不知道是什么问题。
CultureInfo.CurrentCulture
默认情况下,从服务器而不是客户端获取区域性
因此,您部署应用程序的位置可能将语言设置为aa
,而您的本地计算机可能是en
文化
我猜你想了解客户的文化,即浏览你网站的人
如果是,请尝试设置
<system.web>
<globalization culture="auto" uiCulture="auto" />
<system.web>
有关本地化签出的更多信息
你是对的。。。不知怎的,在我的web.config的那一行中,uiCulture被设置为“aa DJ”。这很奇怪,因为我笔记本电脑上的代码很好。无论如何,谢谢。