今天看到一篇帖子,是关于利用全局应用程序类来帮忙获取异常信息,利用 server.Transfer('''')指定接受错误的页面;加上在接受错误页面中利用 server.GetLastError() 获取前一个异常源。
Global.asax 中的Application_Error 函数如下:
- 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();
测试页面Text.aspx中的测试异常代码如下:
- //测试是否捕获了异常信息
- //test1
- //int UserID = Convert.ToInt32(Request["UserID"].ToString());
- //test2
- string Name = "aganar";
- int UID = Convert.ToInt32(Name);