Using IHttpModule Over Global.asax

I was lucky to redo our exception handling system. While I declare that handling exceptions from the point of view of the application is not what we want, as a rule, it is inevitable when our team is understaffed that we need to push the door, so please do not blaze a globalized solution for handling exceptions: )

I had a good hunt to see what common solutions exist. We are currently using Global.asax with the Application_Error event to execute Server.GetLastError (), which is placed in session state and then redirected to another page where the session data is then retrieved and displayed in humanoid format. The redirect also calls sproc, which will carefully check for error information that: a) is sent by email to developers and b) is viewed from a web page accessible only to developers.

The new way I've seen is to use the IHttpModule interface using the class in App_Code to do something on these lines (this is my quick implementation)

Imports Microsoft.VisualBasic Public Class ErrorModule : Implements IHttpModule Public Sub Dispose() Implements System.Web.IHttpModule.Dispose ' Not used End Sub Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init AddHandler context.Error, AddressOf context_Error End Sub Public Sub context_Error(ByVal sender As Object, ByVal e As EventArgs) Dim ex As Exception = HttpContext.Current.Server.GetLastError ' do something with the error ' call the stored procedure ' redirect the user to the error page HttpContext.Current.Server.ClearError() HttpContext.Current.Response.Redirect("index.htm") End Sub End Class 

My question is: what is the advantage of this solution using Global.asax events? Also, what is the best way to transfer data to an error page?

EDIT: the above code works;)

EDIT: Also, how does the HttpModule work behind the scenes? Does it just log an Error event for this particular function when the application starts?

UPDATE:

After much further research, it seems that session data capture is really, really messy when it comes to using the IHttpModule interface. I don’t think that the MS HttpModule has matured enough to be used in our specific scenario - until there are events specific to the session data that it is too dangerous for us.

+4
source share
2 answers

Using a module has the advantage of being easy to remove, all you have to do to disable it is to remove it from <httpModules> in your configuration.

As for your data, try switching from Server.Transfer or Server.RewritePath - this will contain all the current data (including the latest server error).

If for some reason it clears the last error, you can save the error to HttpContext.Items before passing / rewriting, and then extract it after that.

Edit: In response to your editing, the IHttpModule attaches to any relevant events in it IHttpModule.Init .

+6
source

HttpModule basically does the same as Global.asax . It is designed as a more reusable and standalone event processing module.

+1
source

All Articles