HttpContext.Current.Session 为空 asp.net

HttpContext.Current.Session is null asp.net

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

  1. 添加到我的 web.config:
<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 中,我创建了两个事件处理程序:
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# class 来管理我的会话:
 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.

这是我第一次尝试在应用程序中打开会话,因此我需要帮助。 我正在寻找 google 的任何解决方案,但它们要么不起作用,要么不适合我。

有什么想法吗?

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

Microsoft 文档 or on direct link 上有关于此的更多信息。