2013-04-10 58 views
0

我有一个会话,其中包含我在Main.aspx上创建的List(of String)。在我的Global.asax中,执行以下代码以在Timer被触发后检索Session值。尝试获取Global.asax中的会话值,但它是NULL

Private Sub Application_AcquireRequestState(sender As Object, e As EventArgs) 
     If System.Web.HttpContext.Current.Session IsNot Nothing Then 
      If Session("Products") IsNot Nothing Then 
       Response.Write(Session("Products").ToString()) 
      End If 
     End If 
    End Sub 

然而,Session("Products")返回null,Sub退出。我想知道End SubMain.aspx是否会释放Session,因此每当Timer启动时,Session都是空的。有什么想法吗?

+0

您好,也许[这](http://stackoverflow.com/questions/10195791/context-session-object-is-null-in-application-acquirerequeststate)问题可以帮助吗? – nkvu 2013-04-10 16:37:44

回答

0

在你的Main.vb代码中,除了继承的东西之外,一定要继承System.Web.SessionState.IRequiresSessionState接口。

像这样的东西应该工作:

Public Class Main 
    Inherits System.Web.UI.Page, System.Web.SessionState.IRequireSessionState 
' Your application code 
End Class 

这是奇怪的行为虽然为ASP.NET网页应该已经处理会话数据。通常,您在通用处理程序(.ashx)上继承IRequireSessionState,因为它们默认不支持会话数据。

+0

是不是我只能在类中调用'inherits'?因为我已经有'Inherits System.Web.UI.Page'我不能添加'继承System.Web.SessionState.IRequireSessionState'。即使我删除了第一个继承并添加了继承会话,它也显示错误“类只从其他类继承”。 – HShbib 2013-04-11 10:37:26

+0

您可以将您的继承语句更改为“继承System.Web.UI.Page,System.Web.SessionState.IRequireSessionState”。我认为'System.Web.UI.Page'已经继承了'IRequireSessionState'。请注意,VB.NET不支持多类继承,但可以继承多个接口。 – 2013-04-11 12:42:04