2013-04-10 29 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

相關問題