Use the error page to handle errors in a unified manner)

Source: Internet
Author: User

We are working on the Asp.net application.ProgramErrors are usually handled on every page, which wastes time and is not easy to maintain! It is a good solution to uniformly handle unknown exceptions on an error page!

Asp.net provides a solution in the <customerrors> Configuration section, as follows:

<System. Web>

<Customerrors defaultredirect = "~ /Errorpage. aspx"

Mode = "remoteonly">

</Customerrors>

</System. Web>

The defaultredirect attribute is used to indicate the oriented page (defined by the user) When an unhandled error occurs on the ASPX page ), asp.net adds the address of the abnormal page to the address of the error page when directing the error page. ~ /Represents the root directory of the application. For details about the usage of the mode attribute, refer to msdn! In this way, we can use the server. getlasterror () method on the errorpage to obtain the most recent exception, and then write logs and prompt users.

Although the idea is good, there is a problem with implementation. Why? Because Asp.net uses the redirection mechanism to re-navigate the error page, the error information will be lost. That is to say, the exception object we obtain with server. getlasterror () is always empty. Therefore, we can prompt the user for an error and provide a link to return the error page, but we cannot give ourselves a good response.

Fortunately, there is no such thing, and I found another method. If we get the page error information as soon as possible, then we use the server. navigate to the error page in the transer () method. Can you get the error message! The procedure is as follows:

Write the following in the global. asax file:Code:

Protected void application_error (Object sender, eventargs E)
{
Server. Transfer ("~ /Errorpage. aspx ");
}

In this way, we can do the following in the error page:

// Get the latest exception information. The exception type we get is httpunhandledexception, which is a real error package,

// Why does Asp.net do this?

Exception exp = server. getlasterror ();

If (exp! = NULL)

{
This. lblerrormessage. Text = "An error occurred while viewing the page. Please contact the Administrator ";
 

// Save the error details in the internal exception

If (exp. innerexception! = NULL)
{

// Write exception logs, defined by yourself

Writeexceptionlog (exp. innerexception );

}

// Clear the exceptions we have handled to avoid repeated handling.

Server. clearerror ();

}

Well, it's done. Take a closer look. In two ways, the address bar on the wrong page will know why. This problem occurs because the server. Transfer () and response. Redirect () methods are executed differently. I have no plans, but the solution is not satisfactory. If you have a better solution, please let me know.

Now we have found the appropriate processing method ::

You can write error handling code in the global. asax file to avoid the above problems:

Protected void application_error (Object sender, eventargs E)
{
// Log. Write, write something

}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/gentle_wolf/archive/2008/11/27/3394418.aspx

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.