HttpContext.Current.Session为null asp.net

时间:2018-06-18 09:45:41

标签: c# asp.net vb.net session session-state

我遇到了HttpContext.Current.Session的问题。 我尝试在我的应用程序上启用会话状态。 我做了一些事情:

  1. 添加到我的web.config:
  2. <sessionState 
       cookieless="UseCookies"
       regenerateExpiredSessionId="true"
       mode="InProc"
       timeout="20"/>
    

    我也想使用cookies,所以我设置了cookieless =“UseCookies”。

    之后我补充道:

    <pages enableSessionState="true">
    

    此页面部分未关闭,因为它包含一些名称空间:

    <pages enableSessionState="true">
     <namespaces>
       <add namespace="..."/>
     </namespaces>
    </pages>
    
    1. 在我的Global.asax.vb中,我创建了两个事件处理程序:
    2. Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
          ' Fires when the session is started
      End Sub
      

       Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
          ' Fires when the session ends 
       End Sub
      
      1. 我创建了一些c#类,这将是我的会话管理:
      2.  public class MySession
         {
            public static void Add(string key, object value)
            {
                if (HttpContext.Current.Session != null && (HttpContext.Current.Session.Keys == null || (HttpContext.Current.Session.Keys != null && HttpContext.Current.Session[key] == null)))
                {
                    HttpContext.Current.Session.Add(key, value);
                }
            }
        
            public static void Remove(string key)
            {
                ...
            }
        
            public static void Update(string key, object value)
            {
                ...
            }
        
            public static object GetValue(string key)
            {
                ...
            }
        }
        

        这就是我在代码中所做的一切,是的 - 我的应用程序也使用了vb和c#。 因此,当我尝试使用MySession.Add(someKey,someValue)时,我总是得到一个HttpContext.Current.Session == null。

        这是我第一次尝试在应用中开启会话,所以我需要帮助。 我正在寻找谷歌的任何解决方案,但他们要么不工作,要么他们不适合我。

        有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的答案。 我必须使用类似Pipeline的东西(在具有适当代码逻辑的适当位置)。

有关更多信息,请参见StackOverflow link或Microsoft文档上的direct link

相关问题