asp.net 利用Global.asax 捕获整个解决方案中的异常错误

            今天看到一篇帖子,是关于利用全局应用程序类来帮忙获取异常信息,利用 server.Transfer('''')指定接受错误的页面;加上在接受错误页面中利用 server.GetLastError() 获取前一个异常源。

            Global.asax 中的Application_Error 函数如下:

           

[c-sharp]  
 
  1. protected void Application_Error(object sender, EventArgs e)  
  2.        {  
  3.            //捕获整个解决方案下的所有异常  
  4.            try  
  5.            {  
  6.                Server.Transfer("~/Error.aspx");  
  7.            }  
  8.            catch { }  
  9.        }   

            错误接受页面 Error.aspx 获取异常信息的相关代码如下: 

[c-sharp]  
  1. Exception ex = Server.GetLastError().GetBaseException(); //获取异常源  
  2.                if (ex != null)  
  3.                {    
  4.                    Response.Write(ex.Message);      
  5.                }  
  6.                //清空前一个异常  
  7.                Server.ClearError();   

            测试页面Text.aspx中的测试异常代码如下:

           

[c-sharp]   
  1. //测试是否捕获了异常信息  
  2.    //test1  
  3.   //int UserID = Convert.ToInt32(Request["UserID"].ToString());  
  4.   
  5.   
  6.   //test2  
  7.   string Name = "aganar";  
  8.   
  9.   int UID = Convert.ToInt32(Name);  

猜你喜欢

转载自www.cnblogs.com/zgjin/p/8987635.html