【问题标题】:Set session variable in Application_BeginRequest在 Application_BeginRequest 中设置会话变量
【发布时间】:2011-08-24 01:16:01
【问题描述】:

我正在使用 ASP.NET MVC,我需要在 Application_BeginRequest 处设置一个会话变量。问题是此时对象HttpContext.Current.Session 始终是null

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Session != null)
    {
        //this code is never executed, current session is always null
        HttpContext.Current.Session.Add("__MySessionVariable", new object());
    }
}

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 global-asax


【解决方案1】:

也许你可以改变范式......也许你可以使用HttpContext类的另一个属性,更具体地说是HttpContext.Current.Items,如下所示:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpContext.Current.Items["__MySessionVariable"] = new object();
}

它不会将其存储在会话中,但会存储在 HttpContext 类的 Items 字典中,并且将在在特定请求期间可用。由于您在每次请求时都设置它,因此将它存储到“每个会话”字典中确实更有意义,顺便说一下,这正是 Items 的全部内容。 :-)

很抱歉尝试推断您的要求而不是直接回答您的问题,但我之前也遇到过同样的问题,并注意到我需要的根本不是 Session,而是 Items 属性。

【讨论】:

    【解决方案2】:

    您可以这样使用 Application_BeginRequest 中的会话项:

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //Note everything hardcoded, for simplicity!
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("LanguagePref");
    
            if (cookie == null)
                return;
            string language = cookie["LanguagePref"];
    
            if (language.Length<2)
                return;
            language = language.Substring(0, 2).ToLower();   
            HttpContext.Current.Items["__SessionLang"] = language;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language);
    
        }
    
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (context != null && context.Session != null)
            {
                context.Session["Lang"] = HttpContext.Current.Items["__SessionLang"];
            }
        }
    

    【讨论】:

      【解决方案3】:

      尝试在 Global.asax 中的 AcquireRequestState。会话在此事件中可用,每个请求都会触发:

      void Application_AcquireRequestState(object sender, EventArgs e)
      {
          // Session is Available here
          HttpContext context = HttpContext.Current;
          context.Session["foo"] = "foo";
      }
      

      Valamas - 建议编辑:

      成功地将它与 MVC 3 一起使用并避免了会话错误。

      protected void Application_AcquireRequestState(object sender, EventArgs e)
      {
          HttpContext context = HttpContext.Current;
          if (context != null && context.Session != null)
          {
              context.Session["foo"] = "foo";
          }
      }
      

      【讨论】:

      • 这正是我所需要的。
      • 我认为在这种情况下 Session 无论如何都不是正确的属性。尝试我在答案中显示的 context.Current.Items 。自页面生命周期开始以来,它不会为空。
      • 我避免了 MVC3 中的会话问题。我已经根据我的建议编辑了上面的答案。
      猜你喜欢
      • 2015-04-09
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2013-01-27
      • 2018-05-02
      相关资源
      最近更新 更多