大家好,又见面了,我是你们的朋友全栈君。1.创建SessionMgr类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///SessionMgr 的摘要说明
/// </summary>
public class SessionMgr
{
private static IDictionary<string, IDictionary<string, object>> data = new Dictionary<string, IDictionary<string, object>>();
public static IDictionary<string, object> GetSession(string sessionId)
{
if (data.ContainsKey(sessionId))
{
return data[sessionId];
}
else
{
IDictionary<string, object> session = new Dictionary<string, object>();
data[sessionId] = session;
return session;
}
}
public static IDictionary<string, object> GetSession()
{
throw new NotImplementedException();
}
}
2.页面读取服务端Session
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class sessionweb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["MySessionId"] == null)
{
string sessionId = Guid.NewGuid().ToString();
Response.SetCookie(new HttpCookie ("MySessionId",sessionId ));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sessionId = Request.Cookies["MySessionId"].Value;
IDictionary <string ,object > session= SessionMgr.GetSession(sessionId );
session["服务端"] = "333";
}
protected void Button2_Click(object sender, EventArgs e)
{
string sessionId = Request.Cookies["MySessionId"].Value;
IDictionary<string, object> session = SessionMgr.GetSession(sessionId );
Button2 .Text = Convert.ToString(session ["服务端"]);
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159547.html原文链接:https://javaforall.cn