访问请求或响应 OnUnload()?
使用类似的方式存储 cookie
Response.Cookies.Set(new HttpCookie("name","value");
我想在完成页面生命周期后
,因此将其放入 OnUnload()
事件中是有意义的。然而在这个阶段,Request
和 Response
已经被卸载,所以抛出一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 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.