HttpContext.Current.Session 和 Session 的区别
Session(会话)通常指一个动作从开始到结束不间断的一个动作。
例如“打电话”,通常是“1.拿起电话--2.拨对方号码--3.对方截图--4.挂机”。这四个步骤从完成到结束组成了一个基本的Session,中间任何一步断裂,都会导致Session的失效。
而在浏览器里,Session主要通过连接传递,“打开购物--点击连接选择物品--添加到购物车--结账”组成了一个Session,在不使用Cookie的情况下,中间任何一步断裂都会Session失效。
所有,你用浏览器打开2个页面,在一个页面里赋值,在另外一个浏览器取值,是取不到的。只能在一个浏览器通过连接传递(或者通过代码跳转到另外一个页面)而取到值。
HttpContext.Current.Session 和 Session主要是针对浏览器用户,所有,基本上两者基本上没有差别,但是不是所有程序都是通过浏览器访问的。
例如用户通过exe程序获取数据,就可能失效。
另外HttpContext.Current.是针对当前用户,而Session则是针对的web上下文环境里。
例如有2个页面:default.aspx代码如下
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread threadHand1 = new System.Threading.Thread(() =>
{
Session["a"] = "a";
}); threadHand1.Start();
Response.Redirect("default2.aspx");
}
而在另外一个页面获取
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Session["a"]);
}
此时是可以获取到的。
而如果上面代码修改为
protected void Page_Load(object sender, EventArgs e)
{ System.Threading.Thread threadHand1 = new System.Threading.Thread(() =>
{
HttpContext.Current.Session["a"] = "a";
}); threadHand1.Start(); Response.Redirect("default2.aspx"); }
另外一个页面修改为
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(HttpContext.Current.Session["a"]);
}
则获取不到。
因为 default1.aspx里的 System.Threading.Thread 启动的Context并不是default2.aspx里的Context。
总之,除非你开房exe,第三方组件接口等,否则,基本上2者基本上没有区别。
HttpContext.Current.Session 和 Session 的区别的更多相关文章
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
- 问题:HttpContext.Current.Session;结果:Session与HttpContext.Current.Session到底有什么区别呢?
我在做练习的时候遇到了这样一个问题,在母版页页面中写入登录和密码修改的js代码,在登录的方法中写 入 HttpContext.Current.Session.Add("UserPwd&quo ...
- System.Web.HttpContext.Current.Session为NULL解决方法
http://www.cnblogs.com/tianguook/archive/2010/09/27/1836988.html 自定义 HTTP 处理程序,从IHttpHandler继承,在写Sys ...
- HttpContext.Current.Session.SessionID相关问题及备忘
今天Tony提到说我们系统中会利用如下代码来判断用户是否过期. if (string.IsNullOrEmpty(UserContext.ConnectionSessionId)) { LogUIFa ...
- System.Web.HttpContext.Current.Session获取值出错
在自定义类库CS文件里使用System.Web.HttpContext.Current.Session获取Session时提示错误:未将对象引用设置到对象的实例. 一般情况下通过这种方式获取Sessi ...
- 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题
今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...
- System.Web.HttpContext.Current.Session为NULL值的问题?
自定义 HTTP 处理程序,从IHttpHandler继承,在写System.Web.HttpContext.Current.Session["Value"]的时 候,没有问题,但 ...
- asp.net中处理程序调用HttpContext.Current.Session获取值出错
asp.net中处理程序调用System.Web.HttpContext.Current.Session获取Session时提示错误:未将对象引用设置到对象的实例. 解决办法:在处理程序文件类中实现I ...
- HttpContext.Current.Session.Abandon() 大坑 要注意
HttpContext.Current.Session.Abandon(); 如果在调用以上代码之后再存储session 在当前上下文之内是可以访问session的.. 但是页面跳转之后..在其他页面 ...
随机推荐
- Sqoop的安装及简单使用
SQOOP是用于对数据进行导入导出的. (1)把MySQL.Oracle等数据库中的数据导入到HDFS.Hive.HBase中 (2)把HDFS.Hive.HBase中的数据导出到MySQL.Or ...
- ubuntu ifconfig只有lo没有ens33的问题
如果ifconfig只显示了lo, ifconfig -a 却正常显示ens33.那么可以按照如下的操作: service network-manager stop rm /var/lib/Netwo ...
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- 深入分析Spring Boot2,解决 java.lang.ArrayStoreException异常
将某个项目从Spring Boot1升级Spring Boot2之后出现如下报错,查了很多不同的解决方法都没有解决: Spring boot2项目启动时遇到了异常: java.lang.ArraySt ...
- 算法初级面试题01——认识时间复杂度、对数器、 master公式计算时间复杂度、小和问题和逆序对问题
虽然以前学过,再次回顾还是有别样的收获~ 认识时间复杂度 常数时间的操作:一个操作如果和数据量没有关系,每次都是固定时间内完成的操作,叫做常数操作. 时间复杂度为一个算法流程中,常数操作数量的指标.常 ...
- Oracle中Blob和Clob
http://www.cnblogs.com/ztf2008/archive/2009/05/16/1458432.html Blob是指二进制大对象也就是英文Binary Large Object的 ...
- oracle 查询 约束
select * FROM all_constraints where CONSTRAINT_NAME='SYS_xxx'
- BZOJ3956: Count
Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N<=3*10^ ...
- maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet
问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: prope ...