VB.NET:如何从公共类访问会话变量?
我正在开发一个ASP web应用程序,它既有代码隐藏页,也有与任何.ASP页都不关联的单独类。在代码隐藏页面中,我管理几个不同的会话变量,其中一个是会话(“调试”)。例如:VB.NET:如何从公共类访问会话变量?,vb.net,class,session,public,Vb.net,Class,Session,Public,我正在开发一个ASP web应用程序,它既有代码隐藏页,也有与任何.ASP页都不关联的单独类。在代码隐藏页面中,我管理几个不同的会话变量,其中一个是会话(“调试”)。例如: 'Have we created a session debug variabe yet? If not, create one! If Session("debug") IsNot Nothing Then _debug = TryCast(Session("debug"), DebugC
'Have we created a session debug variabe yet? If not, create one!
If Session("debug") IsNot Nothing Then
_debug = TryCast(Session("debug"), DebugControl)
Else
_debug = New DebugControl
Session("debug") = _debug
End If
'This is the debug control. Uncomment this to turn debugging on
_debug.isOn = True
在其中一个公共类中,我尝试在类构造函数中以编程方式使用会话变量:
Public Class SQLControl
Inherits System.Web.UI.MasterPage
Private _debug As DebugControl
Public Sub New()
If HttpContext.Current.Session("debug") Is Nothing Then
_debug = New DebugControl
HttpContext.Current.Session("debug") = _debug
Else
_debug = DirectCast(HttpContext.Current.Session("debug"), DebugControl)
End If
End Sub
但是,当我构建应用程序时,我得到以下结果(第26行突出显示为错误):
我读过几个描述类似问题的线程,但大多数线程都建议使用HttpContext.Current.Session()方法,我已经在这么做了(我也尝试过System.Web.HttpContext.Current.Session,结果相同)。有什么想法吗
谢谢
David如果通过“与任何.asp[sic]页面都不关联的单独类”暗示它们包含Web服务的方法,则可以告诉它您使用了装饰,例如
<WebMethod(EnableSession:=True)> _
Public Function AddToBasket(ByVal filename As String) As String
Return BasketController.AddItem(filename, HttpContext.Current).ToString
End Function
_
公共函数AddToBasket(ByVal文件名为字符串)为字符串
返回BasketController.AddItem(文件名,HttpContext.Current).ToString
端函数
不在参数中传递当前会话是有原因的吗?嗯,每个类中的每个方法都需要写入debugControl的实例化(用于维护第一个故障调试日志),因此在每次函数调用中传递它似乎会带来很大的开销。不,它不是web服务。它只是App_代码目录中的.vb文件。无论如何,我尝试了webMethod,但是遇到了一大堆不同的问题。
<WebMethod(EnableSession:=True)> _
Public Function AddToBasket(ByVal filename As String) As String
Return BasketController.AddItem(filename, HttpContext.Current).ToString
End Function