为什么Cookie在使用Response设置后立即在请求中可用? [英] Why is Cookie available in Request immediately after setting using Response?
问题描述
在网页载入中,如果您在下一行中立即执行Response.Cookies.Add(...),您可以通过Request.Cookies访问该Cookie(...
我知道在封面下,cookie被添加到Request.Cookies by .net,但原始请求从来没有这个cookie。
m是正确的,为什么是这样的?应该不是cookie在下面的请求中可用?不应该立即访问Request.Cookies(...是null?
p>使用
添加Cookie后,您可以使用
HttpResponse.Cookies
集合,
cookie立即可用于
HttpRequest.Cookies
集合,甚至
如果响应未发送到
客户端。
- MSDN上的 HttpRequest.Cookies网页
为什么会这样?猜测,这是因为这是开发人员期望它的工作方式。
In pageload, if you do Response.Cookies.Add(..., immediately in the next line, you can access that cookie via Request.Cookies(...
I know that under the covers, the cookie is added to Request.Cookies by .net, but the original request never had that cookie.
If what I'm saying is correct, why is it this way? Shouldn't the cookie be available in the following request? Shouldn't the immediate access to Request.Cookies(... be null?
You're right, ASP.NET does do that behind the scenes:
After you add a cookie by using the
HttpResponse.Cookies
collection, the cookie is immediately available in theHttpRequest.Cookies
collection, even if the response has not been sent to the client.
-- HttpRequest.Cookies page on MSDN
Why is it like that? At a guess, it's because that's how developers expect it to work.
这篇关于为什么Cookie在使用Response设置后立即在请求中可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!