VB.Net 会话变量简写

问题描述 投票:0回答:1

只是想分享一些我想出的速记函数,以使会话变量不再那么笨拙。 问题:

Session("NameOfSomething","Bob")
Label1.Text = Session("NameOfSomething")
Session.Remove("NameOfSomething")

解决方案:

s("NameOfSomething","Bob")
Label1.Text = s("NameOfSomething")
sr("NameOfSomething")
vb.net
1个回答
0
投票

它是如何实现的:

Module SesVars    
    Public Function s(ByVal SesVarName As String, Optional ByVal SesVal As String = Nothing)
        If SesVal IsNot Nothing Then System.Web.HttpContext.Current.Session("" & SesVarName & "") = SesVal
        If System.Web.HttpContext.Current.Session("" & SesVarName & "") Is Nothing Then s = Nothing : Exit Function
        s = System.Web.HttpContext.Current.Session("" & SesVarName & "")
    End Function
    Public Sub sr(ByVal SesVarName)
        System.Web.HttpContext.Current.Session.Remove("" & SesVarName & "")
    End Sub   
End Module
© www.soinside.com 2019 - 2024. All rights reserved.