Create your own HttpContext class
Brian Bilbro ([email protected]), Wednesday, July 24, 2002
Introduction
Have you ever wanted to be able to retrieve information specific to your application from a component and/or class just like you can get to the common ASP.NET objects (Request, Response, etc...) through the HttpContext.Current static method? It's actually quite easy to accomplish. The easiest solution is to the use the HttpContext.Current.Items collection. However, a cleaner solution is to use the same concept as HttpContext.Current static method.
Let's begin by looking into why creating your own HttpContext.Current is cleaner then HttpContext.Current.Items collection. Occasionally, you might want to be able to access the current Page that you are in from a business component. Code #1 and Code #2 show how this could be possible utilizing the HttpContext.Items collection.
|
|
|
|
YourContext.Current.Page
Without going into the details of creating your own HttpContext.Current yet, let's assume you have already done this. Code #3 shows how you could access your Page object from YourContext.Current static method class.
|
|
Comparing Code #3 to Code #2 you can see that to begin with there is less code. There is also no dependency of knowing the string key name of the page item in the collection. And furthermore you don't need to cast the generic object out of the Items collection into a Page class. And, IMHO, it feels better.
Creating the CoreContext.Current method
The first thing to do when creating your own HttpContext is to determine what are the items (Properties) you would like to expose in your Context object. Continuing with the Page example, let's start with a Page property. However, you probably would like to have a common base page class for all of your pages. Let's call this base page class CorePage and let's call the context class CoreContext. Code #4 shows the realization of this.
|
|
Now, let's add the static method Current (code #5).
|
|
Code #5 reveals the secret of HttpContext.Current. Ok, ILDASM revealed the secret :) Thanks to Dolph Priest for showing me that. HttpContext.Current makes a call to System.Runtime.Remoting.Messaging.CallContext.GetData to retrieve the current HttpContext object for the thread. The same concept is used in CoreContext except it creates a new CoreContext if it doesn't exist in the CallContext.GetData collection. This guarantees that CoreContext.Current will always return a valid instance of CoreContext (i.e. it's never null).
And finally, code #6 shows the CorePage (the base Page). The CorePage sets a reference to itself in the CoreContext.Current class by call the CoreContext internal method SetCorePage.
|
|
Download
The full source to this article is provided in a Visual Studio.NET solution file containing two projects. The first project is the CoreContext and CorePage classes. The second project is a sample ASP.NET Web Application that uses CoreContext and CorePage.
Click Here To Download Article Source
Install Steps:
Conclusion
The source in this article is presented only to demonstrate how you can create your own HttpContext.Current. In your CoreContext class you can make as many properties as you need available to your various components. I've used this concept to make a custom Session object available throughout my application. In another example, I made some specific business "states" available in the custom CoreContext class. Other business components were able to query the custom CoreContext for the current business state without having to pass the information around.