public class CheckAdmin:Page
{
    public CheckAdmin()
    {
        //bool isn = HttpContext.Current == null;//这里是false的
        if (HttpContext.Current.Session["AdminId"] == null || HttpContext.Current.Session["AdminId"].ToString().Length <= 0)//这里报错“未将对象引用”,后面半句去掉也一直,
        {
            HttpContext.Current.Response.Write("<script>alert('你尚未登录,或登录超时!');location.href='~/login.aspx'</sciprt>");
        }
        
    }
}
实在想不通了,大家帮忙看看,在线等

解决方案 »

  1.   

    if (HttpContext.Current.Session["AdminId"] == null )
    这样写也会一直报错?
      

  2.   

    构造函数中 先增加 这个试试 this.Load += new EventHandler(ManagePage_Load);
      

  3.   

    在Global里Session_Start事件中写Session["AdminId"] = null 
      

  4.   

    if (HttpContext.Current.Session["AdminId"] == null || (HttpContext.Current.Session["AdminId"] != null && HttpContext.Current.Session["AdminId"].ToString().Length <= 0)
            {
                HttpContext.Current.Response.Write("<script>alert('你尚未登录,或登录超时!');location.href='~/login.aspx'</sciprt>");
            }
      

  5.   

    Session["AdminId"] 从何而来???
      

  6.   

    是的,现在“bool ise = Session == null;”这句话的异常如下:
    只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<httpModules> 节中包括 System.Web.SessionStateMod 或自定义会话状态模块。说明:这个网站是我在修改,以前是用session验证登录的,我现在想让直接继承这个类就行了,遇到如上问题,而且 我以前也这样做过,
      

  7.   

    是否启用session.enableSessionState =true
    在BasePage里
    public class BasePage : System.Web.UI.Page
        {
           public  BasePage()
           {       }
                 protected override void OnInit(EventArgs O)
           {
               if (base.Session["UserId"] == null || base.Session["UserId"].ToString().Equals(""))
               {
                   Response.Redirect("~/Error.aspx");
               }
           }
         }
      

  8.   

    这个正解 ,我错了,昨天vs不知道感知,我感觉在事件中跟构造函数好像没区别,就没加,请问下这种写法HttpContext.Current.Response.Write("<script>alert('你尚未登录,或登录超时!');location.href='~/login.aspx'</sciprt>");怎么跳转到根目录下的login.aspx??或者怎么获取网站根目录?server.mappath是绝对路径 ,怎么获取像http://localhost:8080/这样的?
      

  9.   


    protected string GetVirtualPath()
        {
            string strServer = Convert.ToString(Request.ServerVariables["SERVER_NAME"]);
            string strPort = Convert.ToString(Request.ServerVariables["SERVER_PORT"]);
            string strRoot = Request.ApplicationPath;
            if (strPort == "80")
                strPort = "";
            return string.Format("http://{0}{1}{2}/", strServer, strPort, strRoot);
    }来源
    http://blog.csdn.net/xuyingchunxyc/archive/2008/09/23/2970000.aspx
      

  10.   

    有一种可能性["AdminId"]为一个对象 且重写了ToString()方法 在这个方法中有空引用抛出
      

  11.   

    谢谢14楼,我直接用js截locaiton.href了,15L说的真有道理,你会重写Session.Tostring()抛出异常么?收工,今天上午可以再测一遍交差
      

  12.   

    楼主没把问题描述清楚吧。你这两种判断报的错是一样的吗?
    你先改web.config文件,把enableSessionState设置为true,然后只判断前半句
    if (HttpContext.Current.Session["AdminId"] == null) 这样报的什么错?