为什么使用 Response 设置后,Request 中的 Cookie 立即可用?

发布于 2024-09-12 15:37:59 字数 201 浏览 1 评论 0原文

在页面加载中,如果您执行 Response.Cookies.Add(...,立即在下一行中,您可以通过 Request.Cookies(...

我知道在幕后,cookie 被添加到 Request.Cookies通过.net,但原始请求从未有过该 cookie。

如果我所说的是正确的,那么为什么在下面的请求中不应该使用 cookie 呢? (...为空?

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?

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

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

发布评论

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

评论(1

梦明 2024-09-19 15:37:59

你是对的,ASP.NET 确实在幕后执行此操作:

使用以下命令添加 cookie 后
HttpResponse.Cookies 收藏,
cookie 立即可用
HttpRequest.Cookies 集合,甚至
如果回复尚未发送至
客户端。

-- MSDN 上的 HttpRequest.Cookies 页面

为什么是这样吗?据猜测,这是因为开发人员希望它是这样工作的。

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.

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