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
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
