IIS7 HttpModule and ISAPI Filter execution order

Kurt Schindler picture Kurt Schindler · Mar 16, 2011 · Viewed 7.9k times · Source

I have a site using ISAPI Rewrite as well as a custom HttpModule that both do Url redirects and rewrites.

In IIS 6, everything worked fine: The ISAPI Rewrite filter would run first, followed by the HttpModule. In IIS 7 (Integrated mode) the order is now the reverse, which poses a problem.

My problem, specifically, is that the HttpModule has a condition where it will issue a Url rewrite using context.RewritePath. It will explicitly add "index.aspx" to the path if no document was provided, so a request to /test/ gets rewritten to /test/index.aspx.

At some point after the path is rewritten, the ISAPI Rewrite filter executes. We've got a rule that does the opposite of the module: a request to /test/index.aspx gets 301-redirected to /test/. Thus, we have an endless loop.

How is the execution order of HttpModules and ISAPI Filters determined in IIS 7? Can the order be changed? I found this question, but it didn't really help. I'm not a master of IIS 7, but I do understand to some extent that modules and ISAPI filters run "together". Unfortunately, they are still administered differently and I can't figure out how to force one to run before the other. Help!

Note: let's assume I cannot change the existing code. It worked in IIS 6. I just want to know if there's a way to make it work in IIS 7 Integrated mode.

Answer

Sergey picture Sergey · May 1, 2012

I've also had a similar problem where I thought that ISAPI rewrites get executed first, but apparently that's not how things work with IIS7

I found this thread that states

In integrated mode the events for native module, isapi and .net are called mixed together

e.g. if it's a BeginRequest then the native module is the first to come, then isapi, then .net code. After that everything will repeat for AuthenticateRequest...

Some time ago it used to be like this: isapi has processed all events, and only after that .net was invoked. This is probably what you've assumed.

http://www.helicontech.com/forum/18447-ISAPI_RW3_Lite_on_IIS_7x_Integrated_mode.html

Hopefully it helps