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)这里错
{
if (HttpContext.Current.Session[strName] != null)
return HttpContext.Current.Session[strName].ToString();
return "";
}这个有错吗?怎么会出现
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。if (HttpContext.Current.Session[strName] != null)这里错
GetSession("")
是ajax后台,还是webservice里?还是在页面基类里?
webservice需要开启WebService会话状态
[WebMethod(EnableSession= true)]
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 "";
}
试试!
{
if (HttpContext.Current.Session != null && HttpContext.Current.Session[strName] != null)
return HttpContext.Current.Session[strName].ToString();
return "";
}
解决了要先判断 HttpContext.Current.Session != null