For a Web application, errors are inevitable. Therefore, we should plan ahead to provide appropriate solutions for possible errors. In fact, a good error handling mechanism is an important criterion for measuring the quality of Web applications. Think about it. If the user accidentally enters the wrong URL in the browser or when the user provides some information that leads to a program error, if we do not handle the problem, instead, the stack information of 404 or 500 error pages or even errors is displayed in front of the user, which will undoubtedly scare some users away. Therefore, we should have a full understanding of the error handling mechanism when developing Web applications.
Let's go back to ASP. NET and raise two questions for everyone to think about: ASP. NET provides several error handling mechanisms for us? If several error handling mechanisms are adopted at the same time, is there a certain priority between them? With this problem, let's take a look at our most common Web. Config file:
- <?xmlversionxmlversion="1.0"?>
- <configuration>
- <system.web>
- <customErrorsmodecustomErrorsmode="On"defaultRedirect="GenericErrorPage.htm">
- <errorstatusCodeerrorstatusCode="403"redirect="Error403.htm"/>
- <errorstatusCodeerrorstatusCode="404"redirect="Error404.htm"/>
- </customErrors>
- </system.web>
- </configuration>
For the <customErrors> setting item, I don't need to talk about it any more. For details, refer to MSDN. The first error handling mechanism-using the <customErrors> configuration item of Web. Config should be the most commonly used.
Next, let's look at another commonly used file: Global. asax. What do you think of when talking about this file? Yes, it is related to the Application and Session of two Web Application objects. Among these events, there is an Error-related event in the Application scope, and the corresponding event processing method is Application_Error. As the name suggests, this event processing method will be called when an application-level error occurs. Therefore, you can add code to this method to handle the error, as shown below:
- protectedvoidApplication_Error(objectsender,EventArgse){
- ExceptionobjErr=Server.GetLastError().GetBaseException();
- Response.Write("Error:"+objErr.Message);
- Server.ClearError();
- }
Here, we should pay attention to the use of the last code Server. ClearError (). Why should we use this code? What if I don't need it? Here I want to sell another token. Well, the second type of error handling mechanism-the Application_Error event handling method in Global. asax is also on stage.
The preceding two error handling methods can be said to be Global. One is from the application configuration file, and the other is the event processing method that must be placed in the Global. asax file under the application root directory. Compared to the global one, it is local, so we naturally think: Is there any error handling mechanism applied to a local page? The answer is "yes", and there are two other methods: Use the ErrorPage attribute and use the Page_Error event processing method. For the first mechanism, you can set the ErrorPage attribute almost anytime to determine the page to which the page will be redirected when an error occurs. For the second mechanism, it is similar to the Application_Error event processing method, except that the trigger time is different. The following are two examples:
- <scriptlanguagescriptlanguage="C#"runat="server">
- protectedvoidPage_Load(objectsender,EventArgse){
- this.ErrorPage="ErrorPage.htm";
-
- }
- </script>
- ProtectedvoidPage_Error (objectsender, EventArgse ){
- Predictionobjerr=Server. GetLastError (). GetBaseException ();
- Response. Write ("Error:" + objErr. Message );
- Server. ClearError (); // pay attention to the use of this Code.
- }
At this point, all four error handling mechanisms have been available, and it is time to rank them. Sort by priority from high to low: Page_Error event handling method> ErrorPage attribute> Application_Error event handling method> <customErrors> configuration item. Although the sorting is like this, there is a subtle relationship between the sorting. First, to enable the ErrorPage attribute to play a role, the mode attribute in the <customErrors> configuration item must be set to "On". Second, although the Page_Error event processing method is at the top, if the Server is missing. clearError () method will still cause lower-priority error handling, that is, the error handling mechanism such as the ErrorPage attribute will still play a role, so that you will not get the expected result. This is also true for the Application_Error event processing method. The order is arranged, but the order is not the most important. It can be said that there is not much significance, because in many cases, you may not mix these four processing mechanisms. I think the most important question is how to use these error handling mechanisms. We hope that experienced users can talk about this issue.
Now, let's take a look at ASP. NET's four error handling mechanisms. The designers of ASP. NET have made comprehensive considerations from the developer's perspective. Therefore, they provide up to four error handling mechanisms for us to choose from. This is commendable. However, when we use an advertisement word-many confusing words, we will also feel dizzy with so many error handling mechanisms. Comparing with the error handling in the J2EE field, we can find that it is relatively simple. First, it corresponds to the <customErrors> Settings. find similar configuration items in the xml file: <errorPage>. Second, in the J2EE field, Page is not an important entity and the event-driven model is not required, therefore, I still cannot find the processing mechanism corresponding to the Application_Error and Page_Error methods. Finally, in the J2EE field, more emphasis is placed on Request and Response, once an error occurs in logic processing, we can easily distribute the Request to the corresponding error processing module through RequestDispatcher. In fact, this is a very flexible way to handle it, if you are interested, please take a look.
- Brief Introduction to ASP applications
- IScriptControl of ASP. NET
- ASP. NET Authentication Service
- Overview ASP. NET Security
- ASP. NET ISAPI