Web page errors are inevitable. How to handle them.
Generally, after a page error occurs, our processing method is generally divided into three steps: business logic → write logs → jump to the overview page or specified page
Solution 1]
1. Add a node in Web. config and specify the redirect page to jump to. The mode is remoteonly, indicating that the yellow pages are reported locally, and other users are redirected.
< System. Web >
< Customerrors Mode = "On" Defaultredirect = "Default. aspx" >
</ Customerrors >
</ System. Web >
2. add error handling in global. asax
Void Application_error ( Object Sender, eventargs E)
{
// Run when an unhandled error occursCode
// Business Processing
// Write logs. The following is an example. We recommend that you use a third-party control log4net.
Try
{
Using (System. Io. streamwriter SW = System. Io. file. appendtext ( " D: \ ax.txt " ))
{
Sw. writeline (server. getlasterror (). innerexception. Message );
Sw. writeline (server. getlasterror (). innerexception. stacktrace );
Sw. Flush ();
}
}
Catch (Exception)
{
}
}
3. If a page error occurs, you must jump to the specified page and rewrite the addparsedsubobject method of the page,
To ensure that the errorpage attribute is assigned at the beginning of page loading, its attribute depends on the mode = "on" in the first step ",
You are not sure whether the mode is remoteonly.
(The default aspxerrorpath = ...)
The lazy can write in the first sentence of the page_load method. If an error occurs before this method, the processing method in step 2 will be used. I am lazy...
Protected Override Void Addparsedsubobject ( Object OBJ)
{
Errorpage = " Log4net. aspx " ;
Base . Addparsedsubobject (OBJ );
}
Append: You can add attribute: errorpage to the front of the page.
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Onerrortest. aspx. CS " Inherits = " Onerrortest " Errorpage = " ~ /Ax. aspx " %>
Solution 2]
1. add error handling in global. asax and specify the response page.
Void Application_error ( Object Sender, eventargs E)
{
// Code that runs when an unprocessed error occurs
// Business Processing
// Write logs. The following is an example. We recommend that you use a third-party control log4net.
Try
{
Using (System. Io. streamwriter SW = System. Io. file. appendtext ( " D: \ ax.txt " ))
{
// Server. getlasterror (). innerexception. tostring () is the exception details.
Sw. writeline (server. getlasterror (). innerexception. tostring ());
Sw. Flush ();
}
}
Finally
{
// Jump to the renewal page
Server. Transfer ( " Default. aspx " );
}
}
2. If a page error occurs, you must jump to the specified page.
Void Page_error ( Object Sender, eventargs E)
{
// Business Processing
// Write logs
// Jump to specified page
// Server. Transfer ("log4net. aspx ");
// Or
// Response. Redirect ("log4net. aspx ");
//Clear the error. If the previous jump page is executed, this sentence will never be executed.
//If not cleared, the application_error () method of Global. asax will be executed.
//Server. clearerror ();
}
We recommend that you use solution 1 because you can modify the default value of redirection in Web. config.