为什么我不能在模块中的HttpContext中设置一个项目然后在我的处理程序中取回它?

时间:2021-08-04 21:12:45

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

在HttpModule中,我在上下文中放置了一个Item,如下所示:

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:

快进到我的实际处理程序(Web表单 - .aspx)。我试着把这个项目拿回来:

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

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

可悲的是,它是NULL - 项目不在那里。

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

我旋转了集合,当它到达我的处理程序时,Items集合有两个键:

  • AspSession
  • AspSessionIDManagerInitializeRequestCalled

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

在模块中设置Item后,我调用RewritePath。不确定这与它有什么关系。

2 个解决方案

#1


Not quite clear about the flow of your code but try using HttpContext.Items instead of HttpContext.Current.Items collection. Check out this link - http://odetocode.com/articles/111.aspx

不太清楚你的代码流,但尝试使用HttpContext.Items而不是HttpContext.Current.Items集合。看看这个链接 - http://odetocode.com/articles/111.aspx

#2


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.

我的问题是那里的重定向偷偷摸摸。因此,我设置值的请求和我读取值的请求实际上是两个单独的请求。它进展得如此之快,即使在调试时也没有注意到。

#1


Not quite clear about the flow of your code but try using HttpContext.Items instead of HttpContext.Current.Items collection. Check out this link - http://odetocode.com/articles/111.aspx

不太清楚你的代码流,但尝试使用HttpContext.Items而不是HttpContext.Current.Items集合。看看这个链接 - http://odetocode.com/articles/111.aspx

#2


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.

我的问题是那里的重定向偷偷摸摸。因此,我设置值的请求和我读取值的请求实际上是两个单独的请求。它进展得如此之快,即使在调试时也没有注意到。