最近在写一个后台,权限验证的代码写入App_Code中的一个类文件qx.cs,验证的条件是session,我用的是string u=HttpContext.Current.Session["user"].ToString();来获取存在session中的用户名,但是运行的时候报错,System.NullReferenceException: 未将对象引用设置到对象的实例。
请问各位,该怎么解决这个问题呢?

解决方案 »

  1.   

    HttpContext.Current.Session["user]
    如果没有设Session["user],是不能转tostring()的
      

  2.   

    if(HttpContext.Current == null )
    {
      throw new Exception (" this  appliction must run in web site ");
    }
    if(HttpContext.Current.Session["user"] == null)
    {
       //Session is null 
    }
      

  3.   

    定义Session了吗??
    Session["user"]=null
    取session
    string   u=HttpContext.Current.Session["user"].ToString()==String.Empty:""?HttpContext.Current.Session["user"].ToString()==String.Empty
      

  4.   

    要先对Session["user"]的值进行判断
      

  5.   

    登陆过后为session["user"]赋过值了.
    然后调用的APP_CODE文件夹里的test.cs里面是这样写的:
    string s = HttpContext.Current.Session["user"].ToString();
      

  6.   

    可能会超时 先判断下手否为null
      

  7.   

    你的session用的是InProc模式, 还是asp.net state或sql server模式?
    如果是InProc模式,会话中设置的值随时有可能会丢失。