Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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: 2.6/5 (5 votes cast)