March 20, 2006

ASP.NET 2.0 and Classes with the HttpContext.Current

A few days ago someone asked me how they could access the Session object from within one of their classes located in the App_Code directory. Should be simple right?

I could just use:

Dim strMyVar as String = Session("myvar")

right?

Nope! Ok so for some reason it works within my aspx code and not the class. What should I do? Well never fear because the HttpContext.Current is here! HttpContext.Current provides access to the current http context (duh) ~ Which means I can not only get the Session object but the Server and Response object as well (among a boat load of others).

So now I need to do a redirect from within my class, here's how simple it is:

HttpContext.Current.Response.Redirect("~/mypage.aspx", true)

Enjoy!!

kick it on DotNetKicks.com

No comments: