HttpContext.Current.Session是空的MVC 3应用程序 [英] HttpContext.Current.Session is null in MVC 3 application

查看:321
本文介绍了HttpContext.Current.Session是空的MVC 3应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双语MVC 3应用程序中,我使用cookie和session保存文化在session_start Global.aspx.cs <方法/ code>文件,而是直接后,会话为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应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆