Response.redirect 不保留 HttpContext.Current.Items [英] Response.redirect does not preserve HttpContext.Current.Items

查看:23
本文介绍了Response.redirect 不保留 HttpContext.Current.Items的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 HttpContext 并发现

HttpContext 对象将为给定的每个请求重新构造到 ASP.Net 应用程序

HttpContext object will be constructed newly for every request given to an ASP.Net application

现在,考虑一个案例,当我有两页时.WebForm1 和 Webform2.在 Form1 中,我正在编写下面提到的代码并重定向到 form2.

Now, consider a case, when I have two pages. WebForm1 and Webform2. In Form1, I am writing the below mentioned code and redirects to form2.

HttpContext.Current.Items.Add("Key", "Value");

查询

当我使用 Server.Transfer 时,这个键仍然存在,而使用 Response.Redirect 时则不是这种情况

Query

When I use Server.Transfer this key persist and this is not the case while using Response.Redirect

无论生成新请求,都会创建 HttpCopntext 对象.此外,会话被保留.这是 HttpContext 的一部分.

Wnenever a new request is generated, HttpCopntext object is created. Moreover, Session is preserved. Which is part of HttpContext.

HttpContext.Current.Session

如果Session可以持久化,为什么Response.Redirect中的HttpContext.Current.Items不行?

If Session can persist, why can't HttpContext.Current.Items in Response.Redirect?

推荐答案

重定向会生成一个新的 HttpContext 这就是其中的项目丢失的原因 - 重定向有效地告诉浏览器下一个 URL请求,当它执行时,它会丢失触发重定向的前一个请求的上下文.

The redirect generates a new HttpContext which is why the items in it are lost - the redirect effectively tells the browser the next URL to request, and when it does it loses the context of the previous request that triggered the redirect.

会话在请求中持续存在(通常使用 sessionID cookie 将用户与服务器上的值联系起来),因此仍然可用.

The session persists across requests (typically using a sessionID cookie to tie the user to values on the server), and is thus still available.

这篇关于Response.redirect 不保留 HttpContext.Current.Items的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆