SkyeBoniwell
SkyeBoniwell

Reputation: 7092

Access page session variables from class

In my web application(vb.net 4.6), I use session variables in all my pages.

It's using

Page.Session As HttpSessionState

I recently created a new public class within this project to hold some helper functions. In that class, I need to check some session variables, but I can't use Page.Session because it's not associated with an .aspx page. It's just a .vb type file.

I tried using

HttpContext.Current.Session

but it's always null.

How can I access my session variables from this new class I created?

Upvotes: 1

Views: 685

Answers (1)

VMAtm
VMAtm

Reputation: 28345

You can implement your helper class as an extension method for HttpSessionState:

Imports System.Runtime.CompilerServices

Module HttpExtensions

    <Extension()> 
    Public Sub Print(ByVal state As HttpSessionState)
        ' a code here
    End Sub

End Module

or simply provide a parameter to your helper class.

Upvotes: 1

Related Questions