ASP.NET: Accessing the Response object in a Class
From Wiki
Summary: Accessing the Response object in a Class
Need help with ASP.NET? Come and ask a question in our ASP.NET Forum
Whilst I'm not a big fan of this method (I don't think the session should be accessed directly from a class as it is then tied to an ASP.NET project and won't work if you tried to use the class in a windows project for example) you can access the session directly if needed.
For example, let's say we want to return a session variable from a public property in our of our classes. Instead of accession "Session" like we would do in a page, we have to use HttpContext.Current.Session to get a reference to the current session object. So, we can come up with this simple example:
- Public Property GetMySessionValue() As String
- Get
- Return HttpContext.Current.Session("mySessionVariable").ToString
- End Get
- Set(ByVal Value As String)
- HttpContext.Current.Session("mySessionVariable") = Value
- End Set
- End Property
This Hack is part of the ASP.NET Hacks collection


