I have a bilingual MVC 3 application, I use cookies and session to save "Culture" in Session_start
method inside Global.aspx.cs
file, but direct after it, the session is null.
This is my code:
protected void Session_Start(object sender, EventArgs e)
{
HttpCookie aCookie = Request.Cookies["MyData"];
if (aCookie == null)
{
Session["MyCulture"] = "de-DE";
aCookie = new HttpCookie("MyData");
//aCookie.Value = Convert.ToString(Session["MyCulture"]);
aCookie["MyLang"] = "de-DE";
aCookie.Expires = System.DateTime.Now.AddDays(21);
Response.Cookies.Add(aCookie);
}
else
{
string s = aCookie["MyLang"];
HttpContext.Current.Session["MyCulture"] = aCookie["MyLang"];
}
}
and second time it goes into the "else clause" because the cookie exists; inside my Filter, when it tries set the culutre, Session["MyCulture"]
is null.
public void OnActionExecuting(ActionExecutingContext filterContext)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(HttpContext.Current.Session["MyCulture"].ToString());
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(HttpContext.Current.Session["MyCulture"].ToString());
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…