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.