Summary
The HttpContext object in the System.Web namespace encapsulates all of the information related to one request and allows you to access that information within or outside of the actual aspx page that is being processed.
A lot of the asp.net code I write goes in class libraries rather than the aspx page so that I can re-use it for multiple pages and sites. However, some readers may have discovered that the Request and Response objects are not available outside of the aspx page.
Actually these objects are available and HttpContext is the way to get at them.
Methods to retrieve the current http related objects: Request, Response, Server, Session, Cache
using System.Web
public static HttpSessionState GetCurrentSessionObject()
{
return HttpContext.Current.Session;
}
public static HttpResponse GetCurrentResponseObject()
{
return HttpContext.Current.Response;
}
public static HttpRequest GetCurrentRequestObject()
{
return HttpContext.Current.Request;
}
public static HttpServerUtility GetCurrentServerObject()
{
return HttpContext.Current.Server;
}
public static Caching.Cache GetCurrentCacheObject()
{
return HttpContext.Current.Cache;
} Back to Top