BUG: IIS7 managed requests

Robert Koritnik picture Robert Koritnik · Aug 12, 2009 · Viewed 8.7k times · Source

(I don't know whether should I also post this question to ServerFault, since it's about IIS configuration?)

In IIS7 we can tell a module to run for managed content (thus speeding up static content serving) by:

<modules>
    ...
    <add name="WhateverName"
         type="WhateverType"
         preCondition="managedHandler"
    ...
</modules>

But. This works fine and dandy as long as there's also a file name (with extension) in the requested URL. If it's omitted it IIS7 will think you want static content and managed modules won't run.

http://localhost/ <-- this one will skip managed handlers
http://localhost/default.aspx <-- this one will run them

If I manually set IIS7 default document, so the first one is default.aspx, I can see no difference there's no difference. To me this looks, walks and sounds like a bug. And it is a bug! Why? Because when I request for the first one, it is a managed request, isn't it. Of course it is. But IIS7 treats it as a static request. So? It's a bug. This request should be treated as managed.

How can I convince IIS7 to run managed handlers for URL requests without file names inside?

Help with thinking

Let me help you a bit with thinking: If I'd reorder system.webServer/handlers, I'm sure could solve this. Before the last StaticFile handler that points to StaticFileModule, DefaultDocumentModule and DirectoryBrowsingModule I should be running integrated asp.net handler on Directory requests. Or write my own handler, that would append default document to any directory request. I'm pretty sure one of these should solve it. But how would I have to configure/develop it?

Answer

Peter Starbek picture Peter Starbek · Aug 25, 2009

The problem is in request-processing order. IIS7 processes requests in order specified by Handlers configuration element of IIS. By default Handlers element of the IIS configuration contains

<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />

at the end of the handlers. Therefore all request that not match any previously specified handler, will be processed by this handler (including folder request too).

You can remove all default handlers by using clear element in handlers configuration and specify your own request processing order.

I recommend to copy default IIS handlers configuration (C:\Windows\System32\inetsrv\config\applicationHost.config) to your web config without StaticFile handler at the end.

Then you should add specific static content handler for each static content type (jpg, gif, js, css).

<add name="StaticFile-swf" path="*.swf" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-png" path="*.png" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-gif" path="*.gif" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-jpg" path="*.jpg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-css" path="*.css" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-js" path="*.js" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />

and manged handler (PageHandlerFactory) for folder requests after that.

<add name="PageHandlerFactory-Folders" path="*" verb="*" type="System.Web.UI.PageHandlerFactory" modules="ManagedPipelineHandler" resourceType="Unspecified" requireAccess="Read" allowPathInfo="false" preCondition="integratedMode" />

At the end you should also add StaticFile handler.

Here is an example.