Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Accessing the Response object in a Class

From Wiki

Jump to: navigation, search

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:

  1. Public Property GetMySessionValue() As String
  2.     Get
  3.         Return HttpContext.Current.Session("mySessionVariable").ToString
  4.     End Get
  5.     Set(ByVal Value As String)
  6.         HttpContext.Current.Session("mySessionVariable") = Value
  7.     End Set
  8. End Property


This Hack is part of the ASP.NET Hacks collection

496 Rating: 5.0/5 (1 vote cast)