Physical Folder Breaks ASP.NET URL Routing on IIS Express
Asked Answered
O

1

8

IIS Express is producing 403.14 Forbidden errors when a URL that would otherwise be handled through ASP.NET URL routing happens to correspond to a physical folder in my ASP.NET project. (The folder contains only code, and it's coincidental that the folder name happens to match the URL of a page; my URL structure is determined dynamically by a database, and users can edit that structure, so although I could just rename my project folder, in general I can't prevent this sort of collision occurring.)

This seems to be happening because the DirectoryListingModule steps in to handle the request, and then promptly fails it because directory browsing is disabled. I've tried removing this:

<system.webServer>
  <handlers>
    <remove name="StaticFile" />
    <add name="StaticFile" path="*" verb="*"
         modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
  </handlers>
</system.webServer>

That removes the default StaticFile handler configuration, which has modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule", and replaces it with a configuration that provides just the feature I want. (I want static file serving, but I have no need for directory listing or default documents in this app.) But the effect seems to be that IIS then produces a completely empty (0 byte) response (with a 200 status) when I hit the offending page.

So next, I tried configuring the StaticFile handler to handle only the specific physical folders that I want to make available:

<system.webServer>
  <handlers>
    <remove name="StaticFile" />
    <add name="StaticFileCss" path="style/*.css" verb="*"
         modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
    <add name="StaticFileScripts" path="Scripts/*" verb="*"
         modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
  </handlers>
</system.webServer>

But when I hit the offending URL, this then produces a 404.4 - Not found error, with a message of The resource you are looking for does not have a handler associated with it.. (The Detailed Error Information on the error page says that we're in the IIS Web Core module, during the MapRequestHandler notification, the handler is Not yet determined, and there's an Error Code of 0x80070002, which is a COM HRESULT that corresponds to the Win32 ERROR_FILE_NOT_FOUND error.)

The mystifying thing is that it's not even bothering to ask ASP.NET whether it has a handler for it. IIS seems to be deciding all by itself that there definitely isn't a handler.

This only happens when there's a folder that matches the URL. All other resources with dynamically-determined URLs work just fine - IIS asks ASP.NET for a handler, ASP.NET's routing mechanism runs as normal, and if the URL corresponds to one of my dynamically defined pages, it all works fine. It's just the presence of a physical folder that stops this all from working.

I can see it's IIS doing this because I get one of the IIS-style error pages for this 404, and they have a distinctive design that's very different from the 404s produced by ASP.NET. (If I try to navigate to a URL that neither corresponds to a physical folder, nor to a dynamic resource, I get a 404 page generated by ASP.NET. So normally, IIS is definitely handing requests over to ASP.NET, but IIS is definitely getting in the way for these problematic resources.)

I tried adding this inside my <system.WebServer>, in case the problem was that IIS has decided that requests corresponding to physical folders do not meet the managedHandler precondition:

<modules runAllManagedModulesForAllRequests="true">

But that doesn't appear to help - it still doesn't get ASP.NET routing involved for URLs that correspond to physical folders. In any case, it would be suboptimal - I would prefer not to have managed handlers run for the content that I definitely want to handle as static content. I effectively want ASP.NET URL routing to be used as a backstop - I only want it to come into play if the URL definitely doesn't refer to static content.

I don't understand why ASP.NET isn't even asking ASP.NET what it thinks in this scenario. Why is it not calling into ASP.NET during the MapRequestHandler phase if there's a physical folder that happens to correspond to the URL?

Oscoumbrian answered 12/7, 2013 at 9:0 Comment(0)
B
3

When a physical file or folder with the same URL as the route is found, routes will not handle the request and the physical file will be served.

Althrough you can change this behavior by setting the RouteExistingFiles Property from the RouteCollection object to true.

Take a look at the MSDN page Scenarios when routing is not applied

Bernardinebernardo answered 1/8, 2013 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.