tags:

views:

41

answers:

1

Hi Guys,

May be I am confused at the moment and not sure at which point or scenario one should use Application_Error (in Global.asax) or Context_Error (when we implement httpModule)

Like...

public void Init(System.Web.HttpApplication context)

{ context.Error += new EventHandler(Context_Error); }

Thanks

A: 

In no case is either an impossible approach and these options are not 100% clear cut. There is more than one way to skin a cat, and having different options gives you greater freedom when designing your application.

When you've logic that is specific to your application and relatively local in how it works, then you are probably better off putting it in global.asax. It is more likely to need to be changed due to changes in the rest of the application, rather than overall improvements to your entire process.

When you've logic you want done in multiple applications on errors, then you are probably better off putting it in a module. It is more likely to need to be changed due to changes to your overall process, rather than specific to that application.

If you want behaviour to be "pluggable" without recompiling, then you need to do it in a module. However, there are other means by which functionality can be turned on or off without recompiling (configuration settings), so even this isn't cut-and-dried.

Jon Hanna
Hi Jon,Thanks for your answer. I am modifying the 404 httpexceptions that are generated by the application to be handled in more SEO way. Default customErrors does the 302 redirects first which doesn't sever the purpose to our SEO team :):). So thought of extending Application_Error or may be writing a custom HttpErrorModule for just one thing. I would guess, in this small thing, Application_Error would be sufficient.Thanks for your quick reply mate.
Nimesh
That's yet a different thing again! There are a few different approaches available, though perhaps the simplest is to set an ASPX page to be executed (not redirected to, both options are available with the latest versions of IIS) in the case of a 404 in IIS.
Jon Hanna
One more thing. If I create a new HttpErroModule and add it, should the execution ever go to Application_Error in Global.asax file. I don't think it should but reconfirming from your expertise would be appreciated.
Nimesh
They both see them, though I can't remember offhand which sees them first. It is possible to clear the errors when handled, so which ever does see it first can cancel block the next from seeing it.Again though, 404 errors are different to exceptions within the application.
Jon Hanna
Yeah the module one gets executed first and then the Applicaion one. Thanks for your help Jon.
Nimesh