1.新建Golbal.asax
void Application_Error(object sender, EventArgs e) { Exception objErr = Server.GetLastError().GetBaseException(); string error = "发生异常页: " + Request.Url.ToString() + "<br>"; error += "异常信息: " + objErr.Message + "<br>"; Server.ClearError(); Application["error"] = error; Response.Redirect("~/ErrorPage/ErrorPage.aspx"); }
2.前台部分:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ErrorPage.aspx.cs" Inherits="ErrorPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ErrorPage</title> <script language="javascript" type="text/javascript"> function CheckError_onclick() { var chk = document.getElementById("CheckError"); var divError = document.getElementById("errorMsg"); if(chk.checked) { divError.style.display = "inline"; } else { divError.style.display = "none"; } } </script> </head> <body> <form id="form1" runat="server"> <div style="text-align: center"> <asp:Label ID="Label1" runat="server" Text="頁面出錯了" Style="text-align: center"></asp:Label><br/> <input type="checkbox" id="CheckError" onclick="CheckError_onclick()" />查看詳細信息<br/><br/> </div> <div id="errorMsg" style="text-align: center; display:none" runat="server"> <asp:Label ID="ErrorMessageLabel" runat="server" Text=""></asp:Label><br /> </div> </form> </body> </html>
3.后台把错误信息显示:
protected void Page_Load(object sender, EventArgs e) { ErrorMessageLabel.Text = Application["error"].ToString(); }