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)这里错

解决方案 »

  1.   

    Seesion里面有strName这个值否?
      

  2.   

    看你这句在哪里写了
    GetSession("")
      

  3.   

    你这代码在哪儿啊,也不说清楚
    是ajax后台,还是webservice里?还是在页面基类里?
      

  4.   

    要是在一般处理程序,就要实现这个接口:  IRequiresSessionState public class ProjectInfo: IHttpHandler, IRequiresSessionState
      

  5.   

    嗯,ajax后台需要实现这个接口,
    webservice需要开启WebService会话状态 
    [WebMethod(EnableSession= true)]
      

  6.   

    异常不是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 "";
            }
    试试!
      

  7.   

           public static string GetSession(string strName)
            {
                if (HttpContext.Current.Session != null && HttpContext.Current.Session[strName] != null)
                    return HttpContext.Current.Session[strName].ToString();
                return "";
            }
    解决了要先判断 HttpContext.Current.Session != null