3 回答

TA貢獻(xiàn)1848條經(jīng)驗 獲得超10個贊
NailItDown和Victor所說的組合。首選/最簡單的方法是使用Global.Asax存儲錯誤,然后重定向到自定義錯誤頁面。
Global.asax:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError();
Application["TheException"] = ex; //store the error for later
Server.ClearError(); //clear the error so we can continue onwards
Response.Redirect("~/myErrorPage.aspx"); //direct user to error page
}
此外,您需要設(shè)置web.config:
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/myErrorPage.aspx">
</customErrors>
</system.web>
最后,根據(jù)您存儲在錯誤頁面中的異常,執(zhí)行您需要的任何操作:
protected void Page_Load(object sender, EventArgs e)
{
// ... do stuff ...
//we caught an exception in our Global.asax, do stuff with it.
Exception caughtException = (Exception)Application["TheException"];
//... do stuff ...
}
- 3 回答
- 0 關(guān)注
- 952 瀏覽
添加回答
舉報