Why is Cookie available in Request immediately after setting using Response?

落花浮王杯 提交于 2019-12-23 11:40:06

问题


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?


回答1:


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 the HttpRequest.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.



来源:https://stackoverflow.com/questions/3406141/why-is-cookie-available-in-request-immediately-after-setting-using-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!