Analysis on making ASP. NET error pages

Source: Internet
Author: User

What is the necessity for creating an ASP. NET error page? After a Web application is published, a custom error page instead of ASP is redirected when an error occurs to give the user a friendly interface and user experience. NET to expose a detailed list of exceptions to users.

Simple ASP. NET error page creation can be set through web. config

The following is a reference clip:

 
 
  1. ﹤customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"﹥   
  2.  ﹤error statusCode="403" redirect="NoAccess.htm" /﹥   
  3.  ﹤error statusCode="404" redirect="FileNotFound.htm" /﹥   
  4. ﹤/customErrors﹥  

To program the cause of an ASP. NET error page, 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)

The following is a reference clip:

 
 
  1. VoidApplication_Error (ObjectSender, EventArgs e)
  2. {
  3. Exception objErr = Server. GetLastError (). GetBaseException ();
  4.  StringError ="Exception page :"+ Request. Url. ToString () +"<Br>";
  5. Error + ="Exception information :"+ ObjErr. Message +"<Br>";
  6. Server. ClearError ();
  7. Application ["Error"] = Error;
  8. Response. Redirect ("~ /ErrorPage. aspx");
  9. }
  10. ErrorPage. aspx
  11.  
  12. Protected VoidPage_Load (ObjectSender, EventArgs e)
  13. {
  14. ErrorMessageLabel. Text = Application ["Error"]. ToString ();
  15. }

When end users use an application, they may not want to know the cause of the error. In this case, we can use the check box to determine whether to display the cause of the ASP. NET error page. You can place the Label in a div and use the check box to determine whether to render the div.

The following is a reference clip:

 
 
  1. ﹤script language="JavaScript" type="text/Javascript"﹥   
  2. ﹤!--   
  3. function CheckError_onclick() {   
  4.  var chk = document.getElementById("CheckError");   
  5.  var divError = document.getElementById("errorMsg");   
  6.  if(chk.checked)   
  7.  {   
  8. divError.style.display = "inline";   
  9.  }   
  10.  else   
  11.  {   
  12. divError.style.display = "none";   
  13.  }   
  14. }   
  15.  
  16. // --﹥   
  17. ﹤/script﹥ 

 

We can make some friendly designs on the errorpage to replace the ASP. NET error page, which makes people look more comfortable.

This article describes how to create an ASP. NET error page.

  1. Analysis of Theme functions in ASP. NET development skills
  2. Analysis of ASP. NET configuration file format
  3. Analysis of ASP. NET Configuration File hierarchy and inheritance
  4. Scope of ASP. NET configuration settings
  5. Analysis of ASP. NET configuration error page

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.