Items Collection

The Items collection, which is a property of the Page class, has one particular quality that distinguishes it from the Session object. It only exists for the lifetime of a single request. That being the case, I’m not really sure what value it adds. If I needed an object in memory over the course of a Page’s life, I would just create a page variable, rather than add a value to the Items collection.

However, the Items collection which is a property of the Page object is different to the Items collection on the HttpContext.Current object. There is definitely a use case for this particular collection, which fills a particular niche.

If you are building one or more HttpModules and you would like to:

  1. store something in memory, but only for the duration of a single request;
  2. be able to retrieve that item in another HttpModule, or in the HttpHandler targeted by the request,

then the HttpContext.Current.Items collection is the way to go.

I’ve prepared a very simple example for download. In that example, there is a simple HttpModule which I have created called NameUserModule. I set a variable called NameOfMe in theĀ HttpApp_BeginRequest event handler and I retrieve it in the Page_Load handler of the Default.aspx page (the targeted HttpHandler for the request).

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>