读书人

HttpContext.Current.Session[strName

发布时间: 2013-05-02 09:39:29 作者: rapoo

HttpContext.Current.Session[strName]未将对象引用设置到对象的实例

       public static string GetSession(string strName)
{
if (HttpContext.Current.Session[strName] != null)
return HttpContext.Current.Session[strName].ToString();
return "";
}

这个有错吗?怎么会出现
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

if (HttpContext.Current.Session[strName] != null)这里错
[解决办法]
Seesion里面有strName这个值否?
[解决办法]
看你这句在哪里写了
GetSession("")
[解决办法]
你这代码在哪儿啊,也不说清楚
是ajax后台,还是webservice里?还是在页面基类里?

[解决办法]
把static去掉试试
[解决办法]
上面的回复不对,看看你这个代码是不是放在构造函数或者其它什么不符合的地方了
[解决办法]
要是在一般处理程序,就要实现这个接口: IRequiresSessionState

public class ProjectInfo: IHttpHandler, IRequiresSessionState
[解决办法]
引用:
要是在一般处理程序,就要实现这个接口: IRequiresSessionState

public class ProjectInfo: IHttpHandler, IRequiresSessionState

嗯,ajax后台需要实现这个接口,
webservice需要开启WebService会话状态
[WebMethod(EnableSession= true)]
[解决办法]
异常不是HttpContext.Current.Session[strName] != null后面的“!=”操作符报错,而是
HttpContext是null 或HttpContext.Current是null
这样判断
public static string GetSession(string strName)
{
if (HttpContext!=null && HttpContext.Current!=null && HttpContext.Current.Session[strName] != null)
return HttpContext.Current.Session[strName].ToString();
return "";
}

试试!
[解决办法]
另外,如果是一般处理程序,楼上的几个方法都得加上去!

读书人网 >asp.net

热点推荐