|
Custom error page
Although the public error messages we send to users are secure, they do not threaten applications.ProgramBut 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:
Configuration > customerrors mode =" remoteonly " defaultredirect =" errorpage. aspx " /> Configuration >
The "defaultredirect" attribute in the <customerrors> tag defines the "default" page to which the user will be redirected when an error occurs. Alternatively, you can alsoCodeStatus, redirect to other pages to overwrite this default value. For example, redirect to a special "file not found" error page, "illegal access" error page, or "server conflict" error page.
For example, the following configuration information overwrites three specific HTTP status codes, and all other errors are returned to a default page:
< Customerrors Defaultredirect = "Http: // anotherhost/error. aspx" Mode = "Remoteonly" > < Error Statuscode = "500" Redirect = "Http:/anotherhost/pages/callsupport.html" /> < Error Statuscode = "404" Redirect = "Http:/anotherhost/pages/adminmessage.html" /> < Error Statuscode = "403" Redirect = "Http:/anotherhost/pages/noaccess.html" /> </ Customerrors >
One thing we have encountered on the custom error page is that although they are very useful for completed situations, they are very difficult to deal with during the development process. It is because you expect that there will be bugs in the development process, and when you find it, you really want to see the actual error information tracking. To solve this problem, the <customerrors> tag supports a "Mode" attribute with three values:
"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.
Code 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;
//Create Event Log if it doesn' t exist
If ( ! EventLog. sourceexists (LOGNAME ))
{
EventLog. createeventsource (LOGNAME, LOGNAME );
}
EventLog = New EventLog ();
Log. Source = LOGNAME; // These are the five options that will display a different icon. 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 );
}
|