I want to use OutputCache to quickly open pages.
I wrote in the controller:
public class HomeController : Controller { [OutputCache(Duration=3600,VaryByParam="none", VaryByCustom="lang")] public ActionResult Index() {
In 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>
At Global.asax:
protected void Application_AcquireRequestState( object sender, EventArgs e ) { //It 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 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 ); }
But I canβt change the language of the site. For example, I switch the language to English, it changes, but then wants to return the Azerbaijani language, it has not changed. What is my mistake? (Sorry for the bad English)
source share