Bartosz
Bartosz

Reputation: 1010

ASP MVC 2 handlers for extensionless urls in IIS 7.5 not working

I have a project with a HttpHandler that's supposed to execute when an extensionless url like this localhost/foo/bar is requested. I got this properly working on a local Visual Studio development server (by using <httpHandlers> in <system.web> instead of <system.webServer><handlers>) but this functionality doesn't work when deployed to IIS 7.5 (Standard 404 error: https://i.sstatic.net/qNx5A.jpg). This issue is not restricted to extensionless URLs (I might add any extension at the end and the issue remains) but my desired functionality is to use extensionless url in this scenario. I googled some information that extensionless URLs may cause some issues that's why I mention it here. The AppPool is set to Integrated. I did aspnet_regiis.exe -i. I have Http Redirection feature installed on the IIS server. Here's my handlers config:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
        <add name="FileDownloadHandler" path="/Home/Files/*" verb="*" type="MvcApplication1.FileDownloadHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
    </handlers>
    <directoryBrowse enabled="true" />
</system.webServer>

And here's my routing setup:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("home/files/{*pathInfo}");

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

I'm guessing the code is fine but something's off with my IIS configuration. Can anyone guide me in the right direction? I've googled for days now and I couldn't find a solution that helped me. Here's a sample project (works on local VS dev server but not on my IIS): http://mapman.pl/MvcApplication1.zip

When you build (and deploy to your local IIS) the above solution try requesting an url like this: http://localhost/MvcApplication1/Home/Files/foobar

Thanks in advance, Bartek

Upvotes: 1

Views: 1264

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

In your web.config replace:

path="/Home/Files/*"

with:

path="Home/Files/*"

The reason for that is because when you host your application in IIS, there's a virtual directory name and the correct path is /MvcApplication1/Home/Files/* instead of /Home/Files/*. This problem is easily solved by using a relative urls in your path attribute.

Upvotes: 1

Related Questions