HttpContext Helper
Need to access an HttpContext object in a class, but can't because the class doesn't inherit System.Web.UI.Page? Here is a simple helper class that will allow you to access some of the most common HttpContext objects.
C#
using
System;
using
System.Web;
using
System.Web.Caching;
using
System.Web.SessionState;
///
///
Encapsulates all HTTP-specific information about an individual HTTP request.
///
public
class
HttpContextHelper
{
///
///
Gets the System.Web.HttpApplicationState object for the current HTTP request.
///
public
static
HttpApplicationState
CurrentApplication
{
get
{
return
HttpContext
.Current.Application; }
}
///
///
Gets the System.Web.Caching.Cache object for the current HTTP request.
///
public
static
Cache
CurrentCache
{
get
{
return
HttpContext
.Current.Cache; }
}
///
///
Gets the System.Web.HttpRequest object for the current HTTP request.
///
public
static
HttpRequest
CurrentRequest
{
get
{
return
HttpContext
.Current.Request; }
}
///
///
Gets the System.Web.HttpResponse object for the current HTTP response.
///
public
static
HttpResponse
CurrentResponse
{
get
{
return
HttpContext
.Current.Response; }
}
///
///
Gets the System.Web.HttpServerUtility object that provides methods used in
///
processing Web requests.
///
public
static
HttpServerUtility
CurrentServer
{
get
{
return
HttpContext
.Current.Server; }
}
///
///
Gets the System.Web.SessionState.HttpSessionState object for the current
///
HTTP request.
///
public
static
HttpSessionState
CurrentSession
{
get
{
return
HttpContext
.Current.Session; }
}
}
VB.NET
Imports
System
Imports
System.Web
Imports
System.Web.Caching
Imports
System.Web.SessionState
'''
''' Encapsulates all HTTP-specific information about an individual HTTP request.
'''
Public
Class
HttpContextHelper
'''
''' Gets the System.Web.HttpApplicationState object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentApplication()
As
HttpApplicationState
Get
Return
HttpContext.Current.Application
End
Get
End
Property
'''
''' Gets the System.Web.Caching.Cache object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentCache()
As
Cache
Get
Return
HttpContext.Current.Cache
End
Get
End
Property
'''
''' Gets the System.Web.HttpRequest object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentRequest()
As
HttpRequest
Get
Return
HttpContext.Current.Request
End
Get
End
Property
'''
''' Gets the System.Web.HttpResponse object for the current HTTP response.
'''
Public
Shared
ReadOnly
Property
CurrentResponse()
As
HttpResponse
Get
Return
HttpContext.Current.Response
End
Get
End
Property
'''
''' Gets the System.Web.HttpServerUtility object that provides methods used in
'''
processing Web requests.
'''
Public
Shared
ReadOnly
Property
CurrentServer()
As
HttpServerUtility
Get
Return
HttpContext.Current.Server
End
Get
End
Property
'''
''' Gets the System.Web.SessionState.HttpSessionState object for the current
'''
HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentSession()
As
HttpSessionState
Get
Return
HttpContext.Current.Session
End
Get
End
Property
End
Class