ASP. NET custom simple error handling page implementation code

Source: Internet
Author: User

The simple error handling page can be set through web. config.
Copy codeThe Code is as follows:
<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"/>
<Error statusCode = "404" redirect = "FileNotFound.htm"/>
</CustomErrors>

If you want to program the cause of the error, you can use the Page_Error event.
The other method can be implemented through Global. asax. I think this method is more convenient. In addition, if you can combine a separate and more friendly page, it seems more comfortable:
Global. asax (you can record the error log if necessary)
Copy codeThe Code is as follows:
Void Application_Error (object sender, EventArgs e)
{
Exception objErr = Server. GetLastError (). GetBaseException ();
String error = "exception page:" + Request. Url. ToString () + "<br> ";
Error + = "exception Message:" + objErr. Message + "<br> ";
Server. ClearError ();
Application ["error"] = error;
Response. Redirect ("~ /ErrorPage. aspx ");
}
ErrorPage. aspx
Protected void Page_Load (object sender, EventArgs e)
{
ErrorMessageLabel. Text = Application ["error"]. ToString ();
}

When end users use applications, they may not want to know the cause of the error. In this case, we can use the check box to check whether the cause of the error is displayed. You can place the Label in a div and use the check box to determine whether to render the div.
Copy codeThe Code is as follows:
<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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.