How To: Create a Strongly-Typed Property For HttpContext.Items
HttpContext.Items is one of my favorite properties in ASP.NET. If I want to communicate a value from the HTTP pipeline to a page and then to a user control, this is my method for doing so. Because the Items property is an implementation of IDictionary, the key/value pair is not strongly-typed. If I am going to access a value often, I would like to make the call as simple as possible. Here is an example of a property I created that "wraps" HttpContext.Items around a key value, yet exposing the value as a string, not object:
const string KEY_ERROR_MESSAGE = "key:errormessage"; public static string ErrorMessage { get { return HttpContext.Current.Items[KEY_ERROR_MESSAGE] as string; }// get set { HttpContext.Current.Items[KEY_ERROR_MESSAGE] = value; }// set }// property