我想使用OutputCache快速打开页面。
我在控制器中写道:
public class HomeController : Controller
{
[OutputCache(Duration=3600,VaryByParam="none", VaryByCustom="lang")]
public ActionResult Index()
{
//........
}
public ActionResult ChangeCulture( string lang, string returnUrl )
{
Session["Culture"] = new CultureInfo( lang );
return Redirect( returnUrl );
}
}
在Layout.cshtml中:
<a href="@Url.Action( "ChangeCulture", "Home", new { lang = "en", returnUrl = this.Request.RawUrl } )">Eng</a>
<a href="@Url.Action( "ChangeCulture", "Home", new { lang = "az", returnUrl = this.Request.RawUrl } )">Az</a>
在Global.asax中:
protected void Application_AcquireRequestState( object sender, EventArgs e )
{
//It's important to check whether session object is ready
if ( HttpContext.Current.Session != null )
{
CultureInfo ci = ( CultureInfo )this.Session["Culture"];
//Checking first if there is no value in session and set default language this can happen for first user's request
if ( ci == null )
{
//Sets default culture to english invariant
string langName = "az";
//Try to get values from Accept lang HTTP header
if ( HttpContext.Current.Request.UserLanguages != null &&
HttpContext.Current.Request.UserLanguages.Length != 0 )
{
//Gets accepted list
langName = HttpContext.Current.Request.UserLanguages[0].Substring( 0, 2 );
}
ci = new CultureInfo( langName );
this.Session["Culture"] = ci;
}
//Finally setting culture for each request
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture( ci.Name );
}
}
public override string GetVaryByCustomString( HttpContext context, string value )
{
if ( value.ToLower() == "lang" )
{
return Thread.CurrentThread.CurrentUICulture.Name;
}
return base.GetVaryByCustomString( context, value );
}
但我不能改变网站语言。例如,我将语言切换为英语,它发生了变化,但后来想要返回阿塞拜疆语,它没有改变。我的错是什么? (抱歉英文不好)
答案 0 :(得分:1)
我解决了自己,改变了
<globalization culture="en" uiCulture="en" />
到
<globalization culture="auto" uiCulture="auto" />
配置文件中的。然后它奏效了。