views:

201

answers:

2

Suppose that both FirstModule and SecondModule handle the Application_BeginRequest event. Will it execute in the order defined in the web.config?

<httpModules>
  <add type="MyApp.FirstModule, MyApp" name="FirstModule"/>
  <add type="MyApp.SecondModule, MyApp" name="SecondModule"/>
  <add type="OtherApp.OtherModule, OtherApp" name="OtherModule"/>
</httpModules>

Are there other ways that the order can be specified?

A: 

I don't think you can guarantee or specify an order that httpmodules will run in. If SecondModule is dependent on FirstModule it may be better to just combine their functionality into 1 httpmodule.

Matt Dearing
I have a 3rd party HttpModule which does UrlRewriting, I would really like to be able to intercept the request before it reaches the 3rd party HttpModule.
jessegavin
You might be able to attach your event handler to an event that gets fired before events that the urlrewriting module cares about. So if UrlRewriting only handles EndRequest you could attach to BeginRequest.
Matt Dearing
+1  A: 

According to this forum post, HttpModules are executed in the order in which they were registered. This makes sense to me, because otherwise the <clear> and <remove> directives would also not work as expected, e.g. when used like this:

<httpModules> 
   <clear/>
   <add... />
</httpModules>
M4N
Someone in the forum post DOES say that. However there were several people who also said that you shouldn't depend on it.
jessegavin
Shouldn't depend on it yes, not because it doesn't work but because of design considerations and promoting loose coupling between the modules. But there are no cases where the ordering of modules will be different to what is in the config file. Hope this helps.
Phil Bennett