【发布时间】:2011-10-09 19:01:41
【问题描述】:
我有一个双语 MVC 3 应用程序,我使用 cookie 和会话将“文化”保存在 Session_start 文件中的 Global.aspx.cs 方法中,但紧随其后,会话为空。
这是我的代码:
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"];
}
}
第二次进入“else 子句”,因为 cookie 存在;在我的过滤器中,当它尝试设置文化时,Session["MyCulture"] 为空。
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());
}
【问题讨论】:
-
有正确答案的相关问题:Calling the Session before any Controller Action is run in MVC @Darin already posted this solution 只是添加它以供参考。
标签: asp.net-mvc-3 session-cookies