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

发布于 2024-07-17 06:12:03 字数 535 浏览 6 评论 0原文

在 HttpModule 中,我将一个 Item 放入 Context 中,如下所示:

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

直接在此代码下(仍在模块内),我可以从集合中检索此字符串,因此我知道它已添加。

快进到我的实际处理程序(Web 表单 - .aspx)。 我尝试取回该项目:

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

遗憾的是,它是 NULL - 该项目不存在。

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

  • AspSession
  • AspSessionIDManagerInitializeRequestCalled

在模块中设置 Item 后,我调用 RewritePath。 不确定这是否与此有关。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-07-24 06:12:03

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

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

箹锭⒈辈孓 2024-07-24 06:12:03

我的问题是重定向潜入其中。 因此,我设置值的请求和我读取值的请求实际上是两个单独的请求。 它进展得太快了,即使在调试时我也没有注意到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文