asp.net custom Simple error Handling page implementation Code _ Practical skills

Source: Internet
Author: User
A simple error-handling page can be set by web.config.
Copy Code code 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 programmatically present the cause of the error, you can do it by page_error the event.
Another way can be achieved by Global.asax, which I think is more convenient, and if you can combine a separate and friendlier page, it seems more comfortable:
Global.asax (Error log can be logged if required)
Copy Code code as follows:

void Application_Error (object sender, EventArgs e)
{
Exception objerr = Server.GetLastError (). GetBaseException ();
String error = "Exception page occurred:" + Request.Url.ToString () + "<br>";
Error + + "Exception information:" + 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 ();
}

When the end user uses the application, they may not want to know the cause of the error, and this time we can do it through the checkbox to see if the cause of the error is present. You can place the label in a Div, and then use the check box to decide whether to render the div.
Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" >
<!-- Br>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>

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.