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.