HttpContext.Current.Session是空的MVC 3应用程序

编程入门 行业动态 更新时间:2024-10-24 04:35:27
本文介绍了HttpContext.Current.Session是空的MVC 3应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个双语MVC 3应用程序中,我使用cookie和session保存文化在session_start 在 Global.aspx.cs 文件,而是直接后,会话为null。

这是我的code:

保护无效在session_start(对象发件人,EventArgs的发送)    {        的HttpCookie aCookie = Request.Cookies时[迈德特];        如果(aCookie == NULL)        {            会话[MyCulture] =去-DE            aCookie =新的HttpCookie(迈德特);            //aCookie.Value = Convert.ToString(会话[MyCulture]);            aCookie [MyLang] =去-DE            aCookie.Expires = System.DateTime.Now.AddDays(21);            Response.Cookies.Add(aCookie);        }        其他        {            字符串s = aCookie [MyLang];            HttpContext.Current.Session [MyCulture] = aCookie [MyLang];        } }

和第二次它进入了else子句,因为cookie存在;我的过滤器,当它试图设置culutre里面,会话[MyCulture] 为null。

公共无效OnActionExecuting(ActionExecutingContext filterContext)    {        System.Threading.Thread.CurrentThread.CurrentUICulture =新System.Globalization.CultureInfo(HttpContext.Current.Session [MyCulture]的ToString());        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(HttpContext.Current.Session[\"MyCulture\"].ToString());    }

解决方案

为什么你在ASP.NET MVC应用程序中使用 HttpContext.Current ? 从不使用它。这是邪恶的,即使在经典的ASP.NET web表单的应用,但在ASP.NET MVC这是一个灾难这需要所有的乐趣了这个漂亮的Web框架。

另外,还要确保你测试值是否在会话present试图使用它,因为我怀疑你的情况是不是之前 HttpContext.Current.Session 是空的,但 HttpContext.Current.Session [MyCulture] 。所以:

公共无效OnActionExecuting(ActionExecutingContext filterContext){    VAR myCulture = filterContext.HttpContext.Session [MyCulture]作为字符串;    如果(!string.IsNullOrEmpty(myCulture))    {        Thread.CurrentThread.CurrentUICulture =新的CultureInfo(myCulture);        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(myCulture);    }}

所以,也许你的问题的根源在于会话[MyCulture] 不正确初始化的在session_start 方法。

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()); }

解决方案

Why are you using HttpContext.Current in an ASP.NET MVC application? Never use it. That's evil even in classic ASP.NET webforms applications but in ASP.NET MVC it's a disaster that takes all the fun out of this nice web framework.

Also make sure you test whether the value is present in the session before attempting to use it, as I suspect that in your case it's not HttpContext.Current.Session that is null, but HttpContext.Current.Session["MyCulture"]. So:

public void OnActionExecuting(ActionExecutingContext filterContext) { var myCulture = filterContext.HttpContext.Session["MyCulture"] as string; if (!string.IsNullOrEmpty(myCulture)) { Thread.CurrentThread.CurrentUICulture = new CultureInfo(myCulture); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(myCulture); } }

So maybe the root of your problem is that Session["MyCulture"] is not properly initialized in the Session_Start method.

更多推荐

HttpContext.Current.Session是空的MVC 3应用程序

本文发布于:2023-11-09 15:51:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Current   HttpContext   MVC   Session

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!