Search code examples
c#web-applicationsasp.net-2.0httpmoduleglobal-asax

What's the performance difference between HttpModule and Global.aspx?


I have made a web app where I am using a module which redirects without "www" urls (http://example.com/) to with "www" urls (http://www.example.com/). But as I am on shared hosting server, where I don't have permission to implement a HttpModule, then I tried the same module code with Global.asax file. That works!

I used the following (Application_BeginRequest()) event to implement my HttpModule functionality.

void Application_BeginRequest()
{
  //module code
}

The module and application is working well and correctly from Global.asax file But I am worried about the performance.

Why we use the HTTPModules in asp.net If we can implement the same using Global.asax file. Is there ay performance differences between both. Or any difference about which I need to worry about when using Global.asax file instead of HttpModule ??

Please explain!


Solution

  • Global.asax inherits from HTTPApplication, and HTTPModules must implement the IHTTPInterface.
    The HTTPModules Init method gets the HTTPApplication object passed in.
    In the Init method you can hook into the events of HTTPApplication.

    I would recommend to use HTTPModules wherever you can.
    Especially if you make shrink-wrapped software where the customer can replace your global.asax with their own.