Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

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
Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Sunday, December 25, 2005 3:44 PMby Ryan Olshan
Filed under: , ,

Comments

No Comments

New Comments to this post are disabled