Wednesday, March 12, 2008

global.asax vs httpmodule

  • The idea is that if you implement a httpmodule, you will need to implement the Ihttpmodule interface. And then you will need to implement the init method of the Ihttpmodule interface.
  • The init method take an object of the httpapplication as a parameter. So that in the init method of this httpmodule class you can hook up an event handler for all the events of the httpapplication object. For example, you can hook up the beginrequest event or error event.
  • But since doing this in a class encourages code reuse because you can compile the module class into a dll and reference it, it is preferable to use httpmodule as oppose to global.asax.
  • You can also separate different logics into different module classes to tidy up the code.
  • You can also add/remove httpmodule by modifiying the web.config without re-compiling
  • The only time you want to use Global.asax is when using OutputCaching with the VaryByCustom property.

HttpModule is used for handling application level events, here are some examples of using httpmodule:

  • User/AD authentication
  • Url rewriting
  • Handling application_error

Reference:

Global.asax? Use HttpModules Instead!

Global.asax or an HttpModule?

blog comments powered by Disqus