asp.net定製簡單的錯誤處理頁面~

來源:互聯網
上載者:User
通常web應用程式在發布後,為了給使用者一個友好介面和使用體驗,都會在錯誤發生時跳轉至一個自訂的錯誤頁面,而不是asp.net向使用者暴露出來的詳細的異常列表。
簡單的錯誤處理頁面可以通過web.config來設定<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>

如果想通過編程的方式來呈現錯誤原因,可以通過Page_Error事件來做這件事.
另一種方式則可以通過Global.asax來實現,我覺得這種方式較為方便,另外如果能結合一個單獨的更加友好的頁面,則看來起更舒服一些

Global.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");
    }

ErrorPage.aspx

    protected void Page_Load(object sender, EventArgs e)
    {
        ErrorMessageLabel.Text = Application["error"].ToString();
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.