Response.redirect 不保留 HttpContext.Current.Items

Response.redirect does not preserve HttpContext.Current.Items(Response.redirect 不保留 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)