Overview ASP. NET Applications

Source: Internet
Author: User

When writing a program, we occasionally fail to handle some exceptions due to negligence. If the exception details are prompted to the user, it will cause unsafe factors. If the details are not prompted, it will cause the user to report the exception. Although you can configure customErrors mode, only the administrator can view the error, but the problem may be found for a long time. With global exception handling, you can immediately record exceptions when exceptions occur, or directly send an email to the Administrator to report the exceptions as quickly as possible.

To add global exception handling for ASP. NET applications, the Error event of HttpApplication is added. This is similar to URL rewriting in the BeginRequest event. First, create a class that inherits System. Web. IHttpModule. Then add the Init method and the ShowError method.

  1. Namespace Snowdream
  2. {
  3. Public class HttpModule: System. Web. IHttpModule
  4. {
  5. Public void Init (System. Web. HttpApplication context)
  6. {
  7. Context. Error + = new EventHandler (ShowError );
  8. }
  9. Private void ShowError (object sender, EventArgs e)
  10. {
  11. // Handle exceptions here
  12. }
  13. }
  14. }

Then you need to configure the httpModules of web. config to make it take effect.

My favorite practice now is to record the exception details in the database, return a number, and redirect the page to the error prompt page written by myself, send this number to the user. If you want to report an error, you only need to provide the error number. The administrator can find and modify the error details in the background.

The following describes two problems I encountered during global exception handling and my solutions.

The first is to obtain the abnormal hResult. Because the private attribute cannot be accessed directly, the solution is to obtain the result through (int) System. Runtime. InteropServices. Marshal. GetHRForException (exception ).

The second problem is to record many errors that are not caused by ASP. NET applications. For example, if you type an incorrect URL, the exception handling system records a "file does not exist ." But these error records not only do not really reflect the exceptions of ASP. NET applications, it will bring more trouble to the Administrator. My solution is to filter these httpexceptions by judging if (exception is System. Web. HttpException.

  1. Introduction to ASP. net mvc Framework
  2. Introduction to MvcAjaxPanel in ASP. NET MVC
  3. ASP. net mvc Framework to save UpdatePanel
  4. Use ASP. net mvc source code to find a solution
  5. ActionInvoker of ASP. net mvc Framework

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.