我有一個雙語的MVC 3應用程序,我使用cookies和會話來保存Global.aspx.cs
文件中Session_start
方法中的「Culture」,但直接在它之後,會話爲空。HttpContext.Current.Session在MVC 3應用程序中爲空
這是我的代碼:
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存在;在我的過濾器中,當它嘗試設置culutre時,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());
}
正確答案的相關問題:[在MVC中運行任何控制器操作之前調用會話](http://stackoverflow.com/questions/3263936/calling-the-session-before-any-controller-action-is -run-in-mvc) @Darin [已發佈此解決方案](http://stackoverflow.com/questions/7705802/httpcontext-current-session-is-null-in-mvc-3-appplication/7705818# 7705818)只是添加它作爲參考。 –