views:

575

answers:

2

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!

A: 

There is pretty much no difference. The point of HTTPModules is for clarity and seperation. Often people will pipe the request through several HTTPModules, which is something you can't get with global.asax.

FlySwat
Ok, that means both methods have no difference in respect of performance. Caz that matters for me :)
Prashant
+3  A: 

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.

Kb
I too prefer the HTTPModules over Global.asax but on my shared hosting server when I am trying to implement HttpModule then it gives me "500 Insternal Server Error". That's why I am going with Global.asax
Prashant
One more thing, I am not getting "Especially if you make shrink-wrapped software where the customer can replace your global.asax with their own." this line of your answers? What is "shrink-wrapped software" and How the customer can replace the global.asax file?
Prashant
At run time, Global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class. If you sell a software package (shrink-wrapped), the customer can change the global.asax
Kb
The HTTP module is compiled and not changeable at runtime
Kb
+1 for recommending the use of HttpModules over HttpApplication (Global).
Cerebrus
Do you think there is much difference between performance of Global and HttpModule? if the difference is of 10-12 then I can go with this, but if the difference is 10-20 then I have to think about this??
Prashant
+1 for better explanation and raising a good point of "parsed and compiled" concept.
Prashant
I would not think the performance differance is something to worry about. They are both using the HTTPApplication object. Only thing is: Every time you change global.asax, it is recompiled.
Kb
"500 Insternal Server Error" could it be a problem in web.config? Maybe you could do a separate posting for that?
Kb