13

I'm using MVC4 and need to route a request like this to a controller:

[myapp]/data/fileinfo.xml

Here is the route I have configured:

routes.MapRoute(
            name: "Data",
            url: "Data/{file}",
            defaults: new { controller = "Data", action = "fileinfo"}
        );

Now, this works perfectly fine and routes requests to my DataController if the URL does not include the .xml extension, but as soon as an extension is used, IIS tries to serve up a static file (instead of routing to my controller) and I get a 404 error.

I've read loads of questions/answers about this issue online, and every solution I've tried has failed.

For example, I've tried using RouteExistingFiles = true when configuring my RouteCollection, and I've added <modules runAllManagedModulesForAllRequests="true" /> in web.config, but to no avail.

If anyone has an idea of what I should try or what I may be missing, it would be much appreciated. I'm using asp.Net 4.5, VS 2012 and IIS 8.0.

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
user2445698
  • 131
  • 1
  • 4

2 Answers2

11

You can add this to your web.config in the <system.webServer><handlers> section:

<add name="ManagedDllExtension" 
     path="data/fileinfo.xml" 
     verb="GET" type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

Your route would be

routes.MapRoute(
        name: "Data",
        url: "Data/fileinfo.xml",
        defaults: new { controller = "Data", action = "fileinfo"}
    );

There is also <modules runAllManagedModulesForAllRequests="true"> but it doesn't seem to work for MVC4/IIS8 (used to be ok in MVC3/IIS7 IIRC). More info here. There is also a performance impact with this one as every request will route through the managed pipeline.

HTH

Sean Kenny
  • 1,626
  • 13
  • 14
  • 1
    Thanks for your response, I really appreciate it. :) After trying this, I end up getting a 500 error when going to the url. The error message says that IIS likely received the request and then failed somehow with the module that handled the request (the error details reference ManagedDllExtension and the ManagedPipelineHandler module, but it's a little too vague to be helpful to me). – user2445698 Jun 03 '13 at 16:48
  • In the meantime, I've found a possible workaround (may not be pretty, but it works). I'm catching /data/fileinfo.xml in Global.asax.cs and using Context.RewritePath() to trim off the extension. Then the routing is able to work properly. Thanks again for your help. – user2445698 Jun 03 '13 at 16:50
  • Ah that's a pity - worked perfectly on my machine :-). Glad to hear you got a workaround. You can always refactor at a later date! – Sean Kenny Jun 03 '13 at 17:02
  • Yeah, my thoughts/hopes exactly. Also, in case anyone else comes across this and attempts my solution, it turned out I did need `` in web.config in addition to my path rewrites. – user2445698 Jun 03 '13 at 22:40
0

I had the same issue with ASP MVC 4. In web.config syste.webserver.handlers section I found the next code:

<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />

All request that have .ext will be handled automatically by IIS. Those were missing in MVC 3.

Radu D
  • 3,505
  • 9
  • 42
  • 69
  • As the name suggests, the all extensionless URLs will be handled by this, whereas the question seems to be more of URLs with extension. Moreover these two handlers would work specifically when IIS app pool is running in classic mode. – Subhash Dike Dec 20 '13 at 12:18