(Original) non-intrusive display of exception information using httpmodule

Source: Internet
Author: User

At work, we often need to maintain some old Asp.net ProgramThese programs do not have a complete exception handling mechanism. In the production environment, Web. config In Customerrors Node Mode Attribute is usually set On In this way, when an error occurs in the production environment, Asp.net The interface is displayed by default, indicating that the program has encountered an error, but the exception information is not available. In this case, to debug the exception, you haveCodeIt is very troublesome to get the local code and use the debug mode or add Exception Handling for the corresponding code. Here I will introduce a simple method. You don't need to get the code, you just need Web. config Add a configuration item and Asp.net Program Bin Directory Copy One DLL You can easily display the exception information.

We can useHttpmoduleUnified intervention and handling(For example:Filter keywords) ASP. NET webform ControlTo build a unified simple Exception Handling Module for the program.

First, create a new project namedErrorprocesshttpmodule, Create a class to implementIhttpmoduleThe Code is as follows:

Public Class Errorprocesshttpmodule:Ihttpmodule

{

Private VoidApplication_errorhandle (ObjectSender,EventargsE)

{

StringMSG =Httpcontext. Current. server. getlasterror (). tostring ();

Httpcontext. Current. response. Write (MSG );

}

 

# RegionIhttpmodule members

Public VoidDispose ()

{

}

 

Public VoidInit (HttpapplicationContext)

{

Context. Error + =New Eventhandler(Application_errorhandle );

}

# Endregion

}

 

The above codeHttpapplicationOfApplication_errorTime Bound to a processing functionApplication_errorhandle,The program is automatically executed when an error occurs.Application_errorFunction to output the error message.

ThenErrorprocesshttpmoduleCompile the project to obtainErrorprocesshttpmodule. dll.

If your program does not have an exception handling mechanism and you need to view the exception information, we can DLL File Copy To the program Bin Directory, and modify Web. config For its < Httpmodules > Add elements to a node as follows:

< Httpmodules >
< Add Name = "Errorprocesshttpmodule" Type = "Errorprocesshttpmodule. errorprocesshttpmodule, errorprocesshttpmodule" />
</ Httpmodules >


This means that when a program fails, the exception information is automatically output.

Well, to output exception information in this way, rememberCustomerrorsNodeModeChangeOn,Otherwise, the program MEETINGAsp.netDisplays the exception information by default.

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.