如何在课堂上获得会话价值? [英] How to get session value in class anywhere ?
本文介绍了如何在课堂上获得会话价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。我在ASP.net中有一些会话如下。在文件Test.aspx.cs中有一些会话
HttpContext.Current.Session [ Id] = obj.USER_MOD_ID;
HttpContext.Current.Session [ Group] = obj.GROUP_MOD_ID;
HttpContext.Current.Session [ Region] = obj.REGION_ID;
然后我创建了一些课程,我希望得到会话的价值。
首先,我创建一个类MySession.cs
public class MySession
{
// 私有构造函数
private MySession()
{
value_group = Group;
// value_id =Id;
// value_region =Region;
}
/ / 获取当前会话。
public 静态 MySession当前
{
获取
{
MySession session =(MySession)HttpContext .Current.Session [ Group];
if (session == null )
{
session = new MySession();
HttpContext.Current.Session [ Group] = session;
}
return session;
}
}
// ****在此添加会话属性,例如:
public string value_group {获得跨度>; set ; }
// public string value_id {get;组; }
// public string value_region {get;组; }
}
其次,我创建了类Test.cs,我希望获得价值会话
string value_group = MySession.Current.value_group.ToString ();
// string value_id = MySession.Current.value_id.ToString();
// string value_region = MySession.Current.value_region.ToString();
但是它会收到错误
< span class =code-sdkkeyword> Object 引用 set 为 object
谢谢大家。
解决方案
MySession session =(MySession)HttpContext.Current.Session [ Group];
这个演员是问题。
请尝试使用它:
MySession session = HttpContext.Current.Session [ 组] as MySession;
如果当前
即时返回会话,更改以下代码
public class MySession
{
// 私有构造函数
私有 MySession()
{
Group = 初始组值;
}
// 获取当前会话。
public static MySession当前
{
get
{
MySession session =(MySession)HttpContext.Current.Session [ MySession的跨度>];
if (session == null )
{
session = new MySession();
HttpContext.Current.Session [ MySession] = session;
}
return session;
}
}
public string 组{ get ; set ; }
}
那么你需要访问会话值为
string value_group = MySession.Current.Group;
Hello guys. I have some sessions in ASP.net as follow . In file Test.aspx.cs have some session
HttpContext.Current.Session["Id"] = obj.USER_MOD_ID;
HttpContext.Current.Session["Group"] = obj.GROUP_MOD_ID;
HttpContext.Current.Session["Region"] = obj.REGION_ID;
And then i create some class , I want to get value of session.
First , i create a class MySession.cs
public class MySession
{
// private constructor
private MySession()
{
value_group = "Group";
//value_id = "Id";
//value_region = "Region";
}
// Gets the current session.
public static MySession Current
{
get
{
MySession session =(MySession)HttpContext.Current.Session["Group"];
if (session == null)
{
session = new MySession();
HttpContext.Current.Session["Group"] = session;
}
return session;
}
}
// **** add your session properties here, e.g like this:
public string value_group { get; set; }
//public string value_id { get; set; }
//public string value_region { get; set; }
}
Second, i create class Test.cs and i want get value of session
string value_group = MySession.Current.value_group.ToString();
//string value_id = MySession.Current.value_id.ToString();
//string value_region = MySession.Current.value_region.ToString();
But it's get error
Object reference not set to an instance of an object
Thank guys.
解决方案
MySession session = (MySession)HttpContext.Current.Session["Group"];
This casting is the issue.
Try using this instead:
MySession session = HttpContext.Current.Session["Group"] as MySession;
if theCurrent
is returning session instant, change the code as below
public class MySession { // private constructor private MySession() { Group= "Initial Group Value"; } // Gets the current session. public static MySession Current { get { MySession session =(MySession)HttpContext.Current.Session["MySession"]; if (session == null) { session = new MySession(); HttpContext.Current.Session["MySession"] = session; } return session; } } public string Group { get; set; } }
then you need to access session value as
string value_group = MySession.Current.Group;
这篇关于如何在课堂上获得会话价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文