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;
///
<summary>/// Encapsulates all HTTP-specific information about an individual HTTP request.
/// </summary>
public class HttpContextHelper
{
/// <summary>
/// Gets the System.Web.HttpApplicationState object for the current HTTP request.
/// </summary>
public static HttpApplicationState CurrentApplication
{
get { return HttpContext.Current.Application; }
} /// <summary>
/// Gets the System.Web.Caching.Cache object for the current HTTP request.
/// </summary>
public static Cache CurrentCache
{
get { return HttpContext.Current.Cache; }
} /// <summary>
/// Gets the System.Web.HttpRequest object for the current HTTP request.
/// </summary>
public static HttpRequest CurrentRequest
{
get { return HttpContext.Current.Request; }
} /// <summary>
/// Gets the System.Web.HttpResponse object for the current HTTP response.
/// </summary>
public static HttpResponse CurrentResponse
{
get { return HttpContext.Current.Response; }
} /// <summary>
/// Gets the System.Web.HttpServerUtility object that provides methods used in
/// processing Web requests.
/// </summary>
public static HttpServerUtility CurrentServer
{
get { return HttpContext.Current.Server; }
} /// <summary>
/// Gets the System.Web.SessionState.HttpSessionState object for the current
/// HTTP request.
/// </summary>
public static HttpSessionState CurrentSession
{
get { return HttpContext.Current.Session; }
}
}
VB.NET
Imports System
Imports System.Web
Imports System.Web.Caching
Imports System.Web.SessionState
'''
<summary>''' Encapsulates all HTTP-specific information about an individual HTTP request.
''' </summary>
Public Class HttpContextHelper
''' <summary>
''' Gets the System.Web.HttpApplicationState object for the current HTTP request.
''' </summary>
Public Shared ReadOnly Property CurrentApplication() As HttpApplicationState
Get
Return HttpContext.Current.Application
End Get
End Property ''' <summary>
''' Gets the System.Web.Caching.Cache object for the current HTTP request.
''' </summary>
Public Shared ReadOnly Property CurrentCache() As Cache
Get
Return HttpContext.Current.Cache
End Get
End Property ''' <summary>
''' Gets the System.Web.HttpRequest object for the current HTTP request.
''' </summary>
Public Shared ReadOnly Property CurrentRequest() As HttpRequest
Get
Return HttpContext.Current.Request
End Get
End Property ''' <summary>
''' Gets the System.Web.HttpResponse object for the current HTTP response.
''' </summary>
Public Shared ReadOnly Property CurrentResponse() As HttpResponse
Get
Return HttpContext.Current.Response
End Get
End Property ''' <summary>
''' Gets the System.Web.HttpServerUtility object that provides methods used in
''' processing Web requests.
''' </summary>
Public Shared ReadOnly Property CurrentServer() As HttpServerUtility
Get
Return HttpContext.Current.Server
End Get
End Property ''' <summary>
''' Gets the System.Web.SessionState.HttpSessionState object for the current
''' HTTP request.
''' </summary>
Public Shared ReadOnly Property CurrentSession() As HttpSessionState
Get
Return HttpContext.Current.Session
End Get
End Property
End Class




