Asp.net custom error page

Source: Internet
Author: User

 

1. custom error page
  
  Although the public error message we send to the user is secure, that is, it does not threaten the secret of the application, but such information is not good-looking. Maybe you want users to never see such information. On the contrary, if an error occurs during request processing, you want to display your "custom error page ", display your own brand and specific error information.

It is easy to add custom error messages to ASP. NET applications. First, write your own web page, which can be any type of Files:. htm,. aspx,. asp, and so on. Modify the configuration information in the config. Web file of the application to point it to the file.

For example, the following configuration information indicates that the browser should be redirected to the "errorpage. aspx" page in case of any unscheduled handling error:

You need to configure in Web. config:

 

<! -- Custom configuration error, not closed, open, details -->
<System. Web>
       <Customerrors mode = "on" defaultredirect = "~ /Errorpage. aspx ">
       <Error statuscode = '000000' redirect = '~ /Erorr500.htm '/>
       <Error statuscode = '000000' redirect = '~ /Erorr403.htm '/>
       <Error statuscode = '000000' redirect = '~ /Erorr404.htm '/>
       <Error statuscode = '000000' redirect = '~ /Erorr505.htm '/>
       </Customerrors>

</System. Web>

 

"On": indicates that custom error pages are always sent;

"Off": indicates that the custom error page is never sent (you always see the original error message );

"Remoteonly": indicates that a custom error page is issued only when a remote browser clicks the site (the developer clicking the site on the actual machine sees the detailed error information ).

 

 

 

2. Add the application error code to the Global. asax file and write it to the system log file.
Protected void application_error (Object sender, eventargs E)
{
Exception lasterror = server. getlasterror ();
String errmessage = lasterror. tostring ();

 

String LOGNAME = "mylog ";
String message = "url" + request. Path + "error:" + errmessage;

 

 

If (! EventLog. sourceexists (LOGNAME ))
{
EventLog. createeventsource (LOGNAME, LOGNAME );
}
EventLog log = new EventLog ();
Log. Source = LOGNAME;
Log. writeentry (message, eventlogentrytype. Information, 1 );
Log. writeentry (message, eventlogentrytype. error, 2 );
Log. writeentry (message, eventlogentrytype. Warning, 3 );
Log. writeentry (message, eventlogentrytype. successaudit, 4 );
Log. writeentry (message, eventlogentrytype. failureaudit, 5 );

 

}

 

Original post address: http://www.cnblogs.com/innhyul/archive/2010/03/26/1696947.html

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.