【发布时间】:2014-05-07 05:41:42
【问题描述】:
我想存储我的全局数据,直到它插入数据库。所以,我想实现一个可以处理创建和存储会话值的客户类。所以,下面是我的代码。
public static class SessionHelper
{
private static int custCode;
public static int PropertyCustCode
{
get
{
return custCode;
}
set
{
if (!string.IsNullOrEmpty(HttpContext.Current.Session[propertyCode].ToString()))
{
custCode = value;
}
else
{
throw new Exception("Property Code Not Available");
}
}
}
public static void MakePropertyCodeSession(int custCode)
{
try
{
HttpContext.Current.Session[propertyCode] = custCode;
}
catch(Exception ex)
{
}
}
我正在从我的网页中分配属性代码,如下所示
SessionHelper.MakePropertyCodeSession(7777);
之后我想访问下面的会话值
int propertyCode=SessionHelper.PropertyCustCode;
但是,我无法访问会话值。每次,我都会得到null 的价值。为什么?我的错在哪里?
【问题讨论】:
-
propertyCode 来自哪里?
-
我通过在加载自身时调用此 MakePpertyCodeSession 方法从我的网页分配此属性代码,此后我想通过使用其属性 (PropertyCustCode) 在整个应用程序中使用此会话值。希望,很清楚
-
尝试在使用静态类的地方添加重要的代码 sn-p
-
我已经更新了我的代码 sn-p。请立即查看。
-
也许我需要休息一下,但你的项目可以编译?
标签: c# asp.net session httpcontext static-class