访问请求或响应 OnUnload()?

发布于 2024-08-23 08:54:52 字数 341 浏览 6 评论 0原文

使用类似的方式存储 cookie

Response.Cookies.Set(new HttpCookie("name","value");

我想在完成页面生命周期后

,因此将其放入 OnUnload() 事件中是有意义的。然而在这个阶段,RequestResponse 已经被卸载,所以抛出一个 null ref 异常。

有人想出头脑风暴来解决这个问题吗?

我能想到的就是将其放入 OnPreRender() 中,但我担心这可能太“快”了。

I want to store a cookie using something like

Response.Cookies.Set(new HttpCookie("name","value");

after I have finished the page lifecycle, so it makes sense to put it in the OnUnload() event.

However at this stage, Request and Response have already been unloaded, so throw a null ref exception.

Has anybody got any brain storms to get around this?

All i can think of is putting it in OnPreRender(), but am worried this may be too 'soon'.

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

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

发布评论

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

评论(1

給妳壹絲溫柔 2024-08-30 08:54:52

由于 cookie 在 HTTP 标头中发送,因此您必须在将任何内容写入响应流之前设置 cookie。因此,您必须在呈现页面之前设置 cookie,因为这会生成在响应中发送的代码。

因此,在页面生命周期之后添加 cookie 就太晚了。无论如何,您认为将 cookie 添加到页面后会有什么不同吗?由于 cookie 在 HTTP 标头中发送,因此无论您何时运行添加它们的代码,它们都会同时到达浏览器。

As the cookies are sent in the HTTP header, you have to set the cookie before anything is written to the response stream. Thus, you have to set the cookie before the page is rendered, as that is what's generating the code that is sent in the response.

So, adding a cookie after the page life cycle is way too late. Why do you think that it makes any difference when you add the cookie to the page anyway? As cookies are sent in the HTTP header, they will arrive to the browser at the same time regardless of when you run the code to add them.

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