ASP.NET error handling

xiaoxiao2021-03-06  20

1.asp.net error handling, mainly used to process "Orthodox Capture of the User"; 2. The following principles about the relationship between "exception manual capture" and "system automatic capture to ASP.NET" in C # : If it is very programs, it can be expected to capture at C # manually, of course, the exception that cannot be expected is to automatically capture. 3. Whether to display user-friendly error messages to users (programmers Custom Error Page), completely controlled by Web.config. 4. At the end of the abnormality, please process in the Applicaton_Error event, Server.getLastError (), you can handle log processing, etc.

ASP.NET has a good new feature: it provides rich support for the processing and tracking of runtime errors. In particular, it provides managers with a very simple way to ensure the fear of "ASP 43433AX" sixteen-based errors will never be displayed in front of our customers. Instead, it allows a more customized information, such as "Sorry, this site is not available". ASP.NET also provides a powerful way to enable developers to equip their code to provide managers with additional information and notices that occur in work site problems. This article details these two technologies, while the end includes a sample code, you can use it directly.

Make sure that the security information ASP.NET is not the same as the previous ASP. When a runtime or compiling time error occurs, an HTML error page is generated. But unlike ASP, ASP.NET is particularly concerned: to ensure that "security" information will be leaked by this error in the default state. Especially if you click on the server from a remote machine. The "Out of the Box" type error handling setting will not cause compiler information of the remote machine, leak configuration information, file name, stack record, source code, or linear data. Conversely, remote users will only see a general information such as "Application Error". To view the wrong details, users must: 1) Click on the page again from the local server, or 2) Modify the configuration settings in the machine or application's config.Web file to allow remote access:

We hope that by setting the default status to we have to "safe", we can ultimately help protect the integrity and security of the application. And thereby correcting many ASP developers - especially ASP managers are reflected in a common complaint / worry).

Using custom error pages Although the public error message we send to users is safe, it does not threaten the secret of the application, but such information is not good. Maybe you want users to never see such information. Conversely, when a request is processed, if an error occurs, you want to display your own "custom error page", show your brand and specific error messages. Adding a custom error message to the ASP.NET application is very easy. First, write your own web page, which can be any type of file: .htm, .aspx, .asp, and so on. The configuration information is then modified in the application's config.Web file to let it point to this file. For example, the following configuration information describes that the browser should be redirected to the "ERRORPAGE.ASPX" page when any failed processing error has occurred.

tag defines that the user will be redirected when an error occurs Default page. Alternatively, you can redirect this default value according to the response HTTP code status. For example: redirect to a special "File" error page, "Illegal Access" error page, "Server Conflict" error page, etc. For example, the following configuration information covers 3 specific HTTP status code, all other errors returns to a default page:

One thing we have encountered on custom error pages, that is, although they are very useful for the completed situation, but it is very difficult to deal with during development. Because you are expected to have bugs during the development process, and when you find it, I really want to see the actual error message tracking. To solve this problem,

tag supports a "Mode" property with 3 values: "ON": Meaning always issues a custom error page; "OFF": meaning never issuing a custom error page (you always see the original error Information); "Remoteonly": It means that only the custom error page is issued only when the remote browser click on the site (and the developer who click on the site on the actual machine sees detailed error message).

Equipment application helps administrator tracking errors Although customized error messages is a good thing, maybe you still hope that when an error occurs on site, developers and administrators can easily find it in real time, and Identify what is problem, URL and exception information are. To solve this problem, ASP.NET introduces a new "application layer" event that can be processed in the global.asax file: "Application_ERROR". During processing a web request, this method is called when there is an unprocessed exception. Developers can obtain special information about the request, such as the URL of the page, query string variable, user agent, cookie's value, etc., and information of the actual exception object of package error message. Then you can continue, run any of what they want to track and to notify the administrator and developers, which may include using System.Diagnostic Apis to write information to NET event log, using System.Web.util SMTP Email APIs send Email to the administrator, write information to a database, and so on. For example, the following Global.asax file demonstrates how to write an error message to a custom NT event log "MyCustomLog", including page URL and exceptional stack record: <% @ Import namespace = "system.diagnostics"%>