current context in when using classes outside a page

I love ASP.NET for web programming. I especially like being able to create classes and use them in the code-behind for a page. The problem I was running into is that I found I needed to access some page-specific properties like Server, Session, Request, Repsonse, etc inside a class. My first inclination was to inherit the System.Web.Page class. This worked but seemed like overkill–there had to be a better way.
A while back, I came across System.Web.HttpContext. This class has a property name Current which basically wires you in to the current HttpContext your code is running in. From there it is easy to get access to Session state, Request variables, etc.
For example, to get the value of a session variable, just use System.Web.HttpContext.Current.Session["variable"] where “variable” is the variable who's value you want.
I figured I'd mention this since I don't recall seeing much on it in the books on ASP.NET I've read or articles I've seen.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s