Search code examples
asp.nethttphandlerhttpmodule

Why can't I set an item in the HttpContext in a module then get it back in my handler?


In an HttpModule, I put an Item in the Context, like this:

HttpContext.Current.Items.Add("MyKey", "Hello world!");

Directly under this code (still inside the module), I can retrieve this string from the collection, so I know it got added.

Fast forward to my actual handler (a Web form -- .aspx). I try to get this item back:

string myString = HttpContext.Current.Items["MyKey"].ToString();

Sadly, it's NULL -- the item is not there.

I spun the collection, and by the time it gets to my handler, the Items collection has two keys:

  • AspSession
  • AspSessionIDManagerInitializeRequestCalled

After I set the Item in the module, I call RewritePath. Not sure if that has anything to do with it.


Solution

  • My problem was that a redirect snuck in there. So the request where I set the values and the request where I read the values were actually two separate requests. It went so fast that I didn't notice, even when debugging.