Asp. NET custom simple error-handling page

Source: Internet
Author: User

Typically, when a Web application is published, in order to give users a friendly interface and experience, they will jump to a custom error page when the error occurs, rather than asp.net a detailed list of exceptions exposed to the user.

A simple error-handling page can be set by web.config.

<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)

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();
}

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.

<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>

We can do some more intimate design on the ErrorPage page to make people look more comfortable.

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.