1.什么是Session?
Session即会话,是指一个用户在一段时间内对某一个站点的一次访问。
Session对象在.NET中对应HttpSessionState类,表示“会话状态”,可以保存与当前用户会话相关的信息。
Session对象用于存储从一个用户开始访问某个特定的aspx的页面起,到用户离开为止,特定的用户会话所需要的信息。用户在应用程序的页面切换时,Session对象的变量不会被清除。
ASP.NET页面是"无状态"的,这意味着每次向服务器发送一个请求,服务器都会生成一个该页面的实例。但有时候,我们希望在不同的页面之间共享信息,比如购物车、用户登录等,于是,ASP.NET为我们提供了一个服务端的Session机制。
对于一个Web应用程序而言,所有用户访问到的Application对象的内容是完全一样的;而不同用户会话访问到的Session对象的内容则各不相同。 Session可以保存变量,该变量只能供一个用户使用,也就是说,每一个网页浏览者都有自己的Session对象变量,即Session对象具有唯一性。
出处:https://blog.csdn.net/lu930124/article/details/43227531
在ASP.NET中,Session只存在于action中,在controller构造函数中获取Session是行不通的。
2. Session是如何工作的?-工作机制;工作流程;
服务端的Session机制是基于客户端的,也就是说服务端的Session会保存每个客户端的信息到服务端内存中。具体过程是这样的:
→客户端向服务端发出请求
→服务端响应客户端,并针对该客户端创建Session和唯一的Session ID
→把Session ID作为key, Session内容作为value,以键值对形式存储到Session State Provider中
→客户端带着专属的Session ID再次向服务端请求
→服务端的Session机制根据客户端的Session ID,从Session State Provider中取出内容返回给客户端
出处:https://blog.csdn.net/mss359681091/article/details/80461237
3.如何使用Session?
1.在Web.config设置相关参数
//inProc模式(缺省模式): 表明会话状态要由ASP.NET存储到内存中,
//而且不用Cookie来传递会话ID。:https://zhidao.baidu.com/question/441754355.html
//其他模式:https://blog.csdn.net/mss359681091/article/details/80461237
//timeout="30";;;30分钟有效期//cookieless="false";;也可以从后台代码中进行配置:Session.TimeOut=30;
也可以从后台代码中进行配置:Session.TimeOut=30;
在Asp.net中,可以有四处设置Session的过期时间:https://www.cnblogs.com/ingstyle/p/5711366.html
2.session的存取
(1)将新的项添加到会话状态中语法格式为:
Session ("键名") = 值
或者
Session.Add( "键名" , 值)
(2)按名称获取会话状态中的值语法格式为:
变量 = Session ("键名")
或者
变量 = Session.Item("键名")
(3)删除会话状态集合中的项语法格式为:
Session.Remove("键名")
(4)清除会话状态中的所有值语法格式为:
Session.RemoveAll()
或者
Session.Clear()
(5)取消当前会话语法格式为:
Session.Abandon()
(6)设置会话状态的超时期限,以分钟为单位。 语法格式为:
Session.TimeOut = 数值
Global.asax 文件中有2个事件应用于Session对象
事件名称 说明
Session_Start 在会话启动时激发
Session_End 在会话结束时激发
3.session-Start-End事件
在ASP.NET中有两个可以使用的Session事件:
Session_Start
Session_End
能处理应用中的这两个事件在global.asax这个文件里,当一个新的Session开启时session_start事件被触发,当Session被回收或是过期时Session_End被触发:
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e){
// Code that runs when a session ends.
}
Ps:
实战博客文章
1:https://blog.csdn.net/weixin_30750335/article/details/95563914
2:https://www.cnblogs.com/KKSoft/p/7242469.html
练习:
1.会话全局之.cshtml
//直接显示在页面@@@@@@@@
@Session["test"];
或者运算
@if (Session["test23"] != null) { Response.Write("9898989898"); };
2.会话全局之不同controller
var controller_test = Session["test"];测试不同controller之间session传值;;;ok
3.会话全局之过滤器使用session
HttpContext.Current.Session["UserID"] = "8686";在过滤器中使用session
namespace JJQ2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult Select()
{
Debug.WriteLine("555555555555555555555555555555555555555555555555555555555");
Response.Write("登录失败");
Session["test"] = "686868";
Session["test"] = "686868";
Session["test"] = "6666";
Session["test1"] = "686868";
Session["test2"] = "686868";
Session["test3"] = "686868";
string ok = "lll";
Debug.WriteLine(Session["test"]);
Debug.WriteLine(Session.Count);
Debug.WriteLine(Session);
//Session.RemoveAll();
//Response.Redirect("aa/Index");
if (Session != null)
{
Debug.WriteLine("55999999999999999999555");
}
var Jsonlist = library.GetList().ToList();//全查
return Json(Jsonlist);
}