Website-level exception capture in Asp.net

Source: Internet
Author: User

Recently, some independent modules have been used in a company's management system. In the implementation of these classes, errors directly throw an exception. I think this is our usual practice. However, the problem also arises because I did not capture these exceptions on the Asp.net page, and these classes are used in multiple pages, therefore, unfriendly error messages are displayed on multiple pages.

What should I do? I don't want to modify the code in one place, which is time-consuming and labor-consuming. In this way, programming will not become physical labor, thus losing pleasure. This reminds me of the custom error page, so Google finally found the answer.


1. Configure web. config:


<CustomErrors mode = "RemoteOnly" defaultRedirect = "ErrorPage. aspx" redirectMode = "ResponseRewrite"/>
The key is the redirectMode attribute. The default value of this attribute is ResponseRedirect. Because it is Redirect, exceptions are ignored, so that you cannot catch exceptions. Here, we can say that the http protocol is a stateless protocol because it is considered to be two requests before and after an error. It is not surprising that the first exception is ignored in the second request. So someone used Session or Application to save the exception. This is not advisable. The correct method is to use "ResponseRewrite ".

2. Output exception information:

We know that when a website error occurs, it will automatically direct to our ErrorPage. Retrial page. How can we access this exception in ErrorPage. aspx? The answer is: Server. GetLastError (), of course, may be Server. GetLastError (). InnerException. This should be analyzed in detail.


If (Server. GetLastError ()! = Null ){
Response. Write (Server. GetLastError (). Message );
}
 

By the way:


Asp.net MVC is much simpler. The framework comes with an Error. aspx. The Exception is saved in Model. Exception, and the Web. config is also configured for us.


The web pages developed since last year are basically MVC, with clear structure, convenient extension, and refreshing HTML code output. It is helpful to check the source code of MVC.

 

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.