views:

1198

answers:

3

(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?

+1  A: 

Removing preCondition="managedHandler" or adding <modules runAllManagedModulesForAllRequests="true"> should do it. The "Preconditions" section of this page has more information.

Jeff Hardy
There's a reason to use preCondition="managedHandler", since it speeds up static content delivery. This is a BAD BAD BAD workaround and not a solution. I removed this setting in the first place.
Robert Koritnik
I thought you *wanted* it to run for static content, but I see that I misread your question.If you store your static content in a separate directory, put a web.config in there that removes the module in question. I usually use a <clear /> and just put in modules I need for static content.
Jeff Hardy
What if content is mixed within your folder?
Robert Koritnik
I prefer to keep static content separate for just that reason. Not much help, I know :).
Jeff Hardy
A: 

You can use a wild card script mapping, but it's inefficient to use the managed handler to handle all requests. The static handler is much more efficient when it is appropriate.

JP Alioto
+1  A: 

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.

Peter Starbek