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.