如题。我在global.asax中进行异常处理,Application_Error中使用response.redirect时有时候会报错,有时候又不会报。
这是什么原因呢?global.asax: <%@ Application Language="C#" %><script runat="server">    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
       
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码
    
            Exception objErr = Server.GetLastError().GetBaseException();
            Application["errorPage"] = Request.Url.ToString();
            Application["errorMsg"] = objErr.Message;
            Server.ClearError();
            Response.Redirect("Error.aspx");//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
            
    
            }    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码
        string str = DateTime.Now.ToString("yyyyMMdd");
        int da = int.Parse(str);
        if (da-20081113>=30)//如果试用期已到,转到错误页面,提示用户注册软件。
        {
            Application["zc"] = "0";
            Response.Redirect("Error.aspx?id=4");
        }
        else
            Application["zc"] = "1";
    }    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。    }
       
</script>

解决方案 »

  1.   

    这样写:Response.Redirect("Error.aspx",false);
    lz给加分么?
      

  2.   

    这跟你的发生错误的页面有关系,比如 a.asp ,b.asp都发生了错误
    如果在a.asp中,没有向客户端写入任何信息,此时就不会报错
    如果在b.asp中,产生错误的时候已经向客户端写入了一些数据比如<html><header>....
    此时将不能够用服务器端的自动跳转进行重定向。
    解决的方法是,要么用客户端的跳转,比如js的跳转
    要么修改b.asp,将写入的首先写入buffer,而非直接写到客户端
      

  3.   

    恩,我想应该:
     void Application_Error(object sender, EventArgs e) 
        { 
            // 在出现未处理的错误时运行的代码        try
            {
                Exception objErr = Server.GetLastError().GetBaseException();
                Application["errorPage"] = Request.Url.ToString();
                Application["errorMsg"] = objErr.Message;
                Server.ClearError();
               
                Response.Write("<script>this.window.location=\"error.aspx\";</script>");
                //Response.Redirect("Error.aspx",true);//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
            }
            catch (Exception e2)
            {
                throw e2; 
            }   
        
                }
      

  4.   

    今天周末
    Response.Redirect("~/Error.aspx");//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
    试试
      

  5.   

    汗了,global.asax中这样写竟然不行:           Response.Write("<script>this.window.location=\"error.aspx\";</script>");
    会报错,好像字符串中不能出现“"</script>"”,为什么啊?
      

  6.   


    //这两句执行的时候有什么区别?第一句是找当前目录下的的Error.aspx,第二句是找当前网站根目录下的Error.aspx,都是同一个文件,其实是一样的吧?
     Response.Redirect("Error.aspx");
     Response.Redirect("~/Error.aspx");//
      

  7.   

    看这里的代码没有用的.其他地方修改了Request,Response里面的东西比如用gzip压缩
    或者  
    执行 Response.Write("") 再执行  Response.Redirect 
      

  8.   

    应该是在这里: protected void Page_Load(object sender, EventArgs e)
        {
            /*
            if (Application["zc"] != null)
            {
                if (Application["zc"].ToString() == "0")
                {
                    Response.Flush();
                    Response.Redirect("Error.aspx?id=4");
                }
            }
            */
            if (Session["info"] != null)
                Session["info"] = "";
            //检查登录状态,如果用户已经登录,给出提示信息
            if (Session["LogState"] != null)
                if (Session["LogState"].ToString() == "1")
                    Response.Redirect("error.aspx?id=2");
            
            txtUser.Focus();
        }
    修改global.asax为:<%@ Application Language="C#" %><script runat="server">    void Application_Start(object sender, EventArgs e) 
        {
            // 在应用程序启动时运行的代码
           
        }
        
        void Application_End(object sender, EventArgs e) 
        {
            //  在应用程序关闭时运行的代码    }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            // 在出现未处理的错误时运行的代码     
                Exception objErr = Server.GetLastError().GetBaseException();
                Application["errorPage"] = Request.Url.ToString();
                Application["errorMsg"] = objErr.Message;
                Server.ClearError();
           
              Response.Redirect("Error.aspx",true);//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
         
        
                }    void Session_Start(object sender, EventArgs e) 
        {
            // 在新会话启动时运行的代码
            try
            {
                string str = DateTime.Now.ToString("yyyyMMdd");
                int da = int.Parse(str);
                if (da - 20081113 >= 30)//如果试用期已到,转到错误页面,提示用户注册软件。
                {
                    Application["zc"] = "0";
                    throw new Exception("试用期已到,请注册软件!"); 
                    
                }
                else
                    Application["zc"] = "1";
            }
            catch (Exception e2)
            {            throw e2; 
            }
        }    void Session_End(object sender, EventArgs e) 
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。    }
           
    </script>
      

  9.   

    楼上的这段代码是在登录页面的。protected void Page_Load(object sender, EventArgs e)
        {
            /*
            if (Application["zc"] != null)
            {
                if (Application["zc"].ToString() == "0")
                {
                    Response.Flush();
                    Response.Redirect("Error.aspx?id=4");
                }
            }
            */
            if (Session["info"] != null)
                Session["info"] = "";
            //检查登录状态,如果用户已经登录,给出提示信息
            if (Session["LogState"] != null)
                if (Session["LogState"].ToString() == "1")
                    Response.Redirect("error.aspx?id=2");
            
            txtUser.Focus();
        }
      

  10.   

    还是把注册的提示信息放到login页面比较保险。
    如果放到global.asax,用户看到提示信息后,再手工输入login.aspx的话,就会跳过session_start了。 try
            {
                string str = DateTime.Now.ToString("yyyyMMdd");
                int da = int.Parse(str);
                if (da - 20081113 >= 30)//如果试用期已到,转到错误页面,提示用户注册软件。
                {
                    Application["zc"] = "0";
                    throw new Exception("试用期已到,请注册软件!"); 
                    
                }
                else
                    Application["zc"] = "1";
            }
            catch (Exception e2)
            {            throw e2; 
            }