如果在一个B/S项目代码里没有执行try……catch,但又需要每次发生异常都跳转到一个友好的提示页面,而该项目中有上百个页面,不可能每个页面都再去加try……catch,请问你如何解决?

解决方案 »

  1.   

    在Global.asax中写:
    void Application_Error(object sender, EventArgs e)
    {
    //在出现未处理的错误时运行的代码
    Response.Redirect("友好提示页面.html");
    }
      

  2.   


    web.config怎么实现?看那个传送门内容没看懂。
      

  3.   

    自定义错误页Error.aspx,通过webconfig.xml的错误配置,或在Global.asax捕获整个解决方案中的异常错误。
    webconfig.xml:
    <configuration>
     
        <system.web>
     
            <customErrors mode="On" defaultRedirect="Error.aspx"/>
     
        </system.web>
     
    </configuration>Global.asax:
    protected void Application_Error(object sender, EventArgs e)  
    {  
       try  
        {  
         Server.Transfer("Error.aspx");  
        }  
       catch { }  
    } Error.aspx:
    Exception ex = Server.GetLastError().GetBaseException(); 
    if (ex != null)  
    {    
         Response.Write(ex.Message);      
    }  
    Server.ClearError();