ASP. NET provides three main methods for capturing and responding to errors when an error occurs: page_error event, application_error event, and ApplicationProgramConfiguration file (Web. config ).
If you do not call server. clearerror or capture an error in the page_error or application_error event, the error is handled based on the setting in the <customerrors> section of the web. config file. In the <customerrors> section, you can specify the redirection page as the default error page (defaultredirect) orCodeSpecify a specific page. You can use this method to customize the error message you receive.
Although you can reference the default error page in the value of the defaultredirect attribute in the <customerrors> section, you can also specify the specific page to be redirected Based on the HTTP Error code. <Error> this option is allowed for child elements.
<Customerrors mode = "on" defaultredirect = "applicationerroy. aspx">
<Error statuscode = "403" Redirect = "403.htm"/>
<Error statuscode = "404" Redirect = "404.htm"/>
<Error statuscode = "500" Redirect = "500.htm"/>
</Customerrors>
Asp.net custom error handling PAGE method 1
1. Add <customerrors mode = "on" to Web. config and <system. Web> </system. Web>"
Defaultredirect = "applicationerroy. aspx"> </customerrors> node,
2. add error handling page: applicationerroy. aspx calls the following method:
Private void dealerroy ()
{
Httpexception erroy = new httpexception ();
String strcode = erroy. errorcode. tostring ();
String strmsg = erroy. message;
Erroy. helplink = "Sss ";
Response. Write ("errorcode:" + strcode + "<br> ");
Response. Write ("message:" + strmsg + "<br> ");
Response. Write ("helplink:" + erroy. helplink + "<br> ");
Response. Write ("Source:" + erroy. Source + "<br> ");
Response. Write ("targetsite:" + erroy. targetsite + "<br> ");
Response. Write ("innerexception:" + erroy. innerexception + "<br> ");
Response. Write ("stacktrace:" + erroy. stacktrace + "<br> ");
Response. Write ("gethtmlerrormessage:" + erroy. gethtmlerrormessage () + "<br> ");
Response. Write ("erroy. gethttpcode (). tostring ():" + erroy. gethttpcode (). tostring () + "<br> ");
Response. Write ("erroy. Data. tostring ():" + erroy. Data. tostring () + "<br> ");
}
Private void dealerroy ()
{
Httpexception erroy = new httpexception ();
String strcode = erroy. errorcode. tostring ();
String strmsg = erroy. message;
Erroy. helplink = "Sss ";
Response. Write ("errorcode:" + strcode + "<br> ");
Response. Write ("message:" + strmsg + "<br> ");
Response. Write ("helplink:" + erroy. helplink + "<br> ");
Response. Write ("Source:" + erroy. Source + "<br> ");
Response. Write ("targetsite:" + erroy. targetsite + "<br> ");
Response. Write ("innerexception:" + erroy. innerexception + "<br> ");
Response. Write ("stacktrace:" + erroy. stacktrace + "<br> ");
Response. Write ("gethtmlerrormessage:" + erroy. gethtmlerrormessage () + "<br> ");
Response. Write ("erroy. gethttpcode (). tostring ():" + erroy. gethttpcode (). tostring () + "<br> ");
Response. Write ("erroy. Data. tostring ():" + erroy. Data. tostring () + "<br> ");
}
This method cannot completely display the error message;
Asp.net custom error handling PAGE method 2
1. Add <customerrors mode = "on" to Web. config and <system. Web> </system. Web>"
Defaultredirect = "applicationerroy. aspx"> </customerrors> node,
2. Add the global. asax file, locate the application_error event, and add the following code:
// This is the addition of a global application class to handle error pages in application_error events and web. config. The error page can be located even if there is no web. config.
Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs
Exception erroy = server. getlasterror ();
String err = "error page:" + request. url. tostring () + "</BR> ";
Err + = "exception information:" + erroy. Message + "</BR> ";
Err + = "Source:" + erroy. Source + "</BR> ";
Err + = "stacktrace:" + erroy. stacktrace + "</BR> ";
// Clear the previous exception
Server. clearerror ();
// This process uses session ["proerror"] error. So application ["proerror"]
Application ["erroy"] = err;
// Response. Redirect ("../frmsyserror. aspx") is not used in the page ");
System. Web. httpcontext. Current. response. Redirect (httpcontext. Current. Request. applicationpath + "/applicationerroy. aspx ");
}
2. Add the following code to the error handling page: applicationerroy. aspx;
Protected void page_load (Object sender, eventargs E)
{
// Display the error codes in the program
If (! Ispostback)
{
// Display the error codes in the program
If (application ["erroy"]! = NULL)
{
Response. Write (application ["erroy"]. tostring ());
}
}
}
This method can completely Display Error information,
The best way is to use the two methods together!
In addition, you can also set
<Customerrors mode = "on" defaultredirect = "applicationerroy. aspx">
<Error statuscode = "403" Redirect = "403.htm"/>
<Error statuscode = "404" Redirect = "404.htm"/>
<Error statuscode = "500" Redirect = "500.htm"/>
</Customerrors>
Add
HTTP Error Code Description:
"403": forbidden
"404": not found
"500": Internal Server Error
Method 3 page_error event
The page_error event provides a method to capture page-level errors. You can only Display error messages (as shown in the following sample code), or record events or perform other operations.
Note: This example displays detailed error information in the browser. This example is only for instructions. Be careful when displaying detailed information (especially when the application is running on the Internet) to the end user of the application. A more appropriate way is to display a message to the user, tell the user that an error has occurred, and then record the detailed error information in the event log.
In this example, an empty error occurs in the page_load event. Follow these steps to create an initial page that will test the page_error event. 1. Follow these steps to add a new file named pageevent. aspx to the project: A. Open Microsoft Visual Studio. NET.
B. In Solution Explorer, right-click the project node, point to add, and click Add web form.
C. In the Name text box, type pageevent. aspx and click open.
Add the following code to pageevent. aspx:
<Script language = C # runat = "server">
Void page_load (Object sender, system. eventargs E)
{
Throw (New argumentnullexception ());
}
Public void page_error (Object sender, eventargs E)
{
Exception objerr = server. getlasterror (). getbaseexception ();
String err = "<B> error caught in page_error event </B> <HR> <br>" +
"<Br> <B> error in: </B>" + request. url. tostring () +
"<Br> <B> error message: </B>" + objerr. Message. tostring () +
"<Br> <B> stack trace: </B> <br>" +
Objerr. stacktrace. tostring ();
Response. Write (ERR. tostring ());
Server. clearerror ();
}
</SCRIPT>
From the File menu, click Save pageevent. aspx.
Right-click the page and click View in the browser to run the page. Please note that errors will be thrown and reported according to code specifications.
Note: You may notice that the Code calls server. clearerror. This prevents the error from continuing to the application_error event to be processed.
Note the inherits attribute in the @ page command. If you have set inherits, you must first generate the project and then browse to the page. If the project is not generated first, the following error message is displayed: 'Project. pageevent' is not a valid type