Two solutions for ASP. NET error handling _ ax

Source: Internet
Author: User
Tags log4net

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.

 

Related Article

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.