Response.redirect does not preserve HttpContext.Current.Items(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Response.redirect 不保留 HttpContext.Current.Items
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
