如何HttpContext的被保持在请求响应 [英] How is HttpContext being maintained over request-response
问题描述
我想知道HttpContext的是如何保持鉴于幅材的请求 - 响应性质基本上是无状态的。
I am wondering how the HttpContext is maintained given that the request-response nature of the web is essentially stateless.
被发送的__EVENTTARGET / __EVENTARGUMENTS的一部分隐藏字段,以便类的httpRuntime可以通过请求阅读本节创建HttpContext类标识符正在为HttpContext对象(的HttpWorkerRequest)?我不认为
Is an identifier being for the HttpContext object being sent as part of the __EVENTTarget / __EVENTARGUMENTS hidden fields so that the HttpRuntime class can create the HttpContext class by reading this section from the request (HttpWorkerRequest)? I don't think
请让我知道,因为我试图填补一些漏洞在我的HTTP管道的理解和我无法找到有关此的任何信息。
Please let me know as I am trying to fill some holes in my understanding of the http pipeline and I was unable to find any information about this.
我理解是这样
HttpContext.Current.Session [的myKey] =值;
I understand something like HttpContext.Current.Session["myKey"] = Value;
只是工作,但如果我不得不做不同的语言(比如perl的)类似的东西,我将不得不使用隐藏字段一样的,是不是?
just works but if I had to do something similar in a different language (say perl), I would have to use hidden fields for the same, wouldn't I?
谢谢
-Venu
Thanks -Venu
推荐答案
HttpContext的是重新创建每个请求。 HttpSession中,但是,将存储在请求的服务器上。基本上,HttpSession的是一个字典<字符串,字典<字符串对象>&取代。初始密钥,会话ID,或者由Cookie或查询字符串参数(如果使用cookie的会话)提供。如果您使用Fiddler,你会看到包含该用户的会话密钥的ASP.NET_SessionId的cookie。
The HttpContext is recreated for each request. The HttpSession, however, is stored on the server across requests. Basically, HttpSession is a Dictionary<string, Dictionary<string, object>>. The initial key, the session id, is provided by either a cookie or a query string parameter (if using cookie-less sessions). If you use Fiddler, you'll see the ASP.NET_SessionId cookie that contains the key for that user's session.
在code:
class HttpSessionState {
private static readonly Sessions =
new Dictionary<string, Dictionary<string, object>>();
public object this(string key) {
get {
return GetCurrentUserSession()[key]
}
set {
GetCurrentUserSession()[key] = value;
}
}
private Dictionary<string, object> GetCurrentUserSession() {
var id = GetCurrentUserSessionId[]
var d = Sessions[id];
if (d == null) {
d = new Dictionary<string, object>();
Sessions[id] = d;
}
return d;
}
private string GetCurrentUserSessionId() {
return HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;
}
}
真正实施还处理会话超时,放弃和Cookie会话 - 但基本思想是一致的。
The real implementation also handles session timeouts, abandons, and cookieless sessions - but the basic idea is the same.
这篇关于如何HttpContext的被保持在请求响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!