ASP.NET MVC - Routing - an action with file extension

Jakub Holovsky picture Jakub Holovsky · Mar 4, 2014 · Viewed 17.6k times · Source

is there a way to achieve calling URL http://mywebsite/myarea/mycontroller/myaction.xml This would basically "fake" requesting a file but the result would be an action operation that would serve a file created dynamically?

I tried this:

context.MapRoute(
                "Xml_filename",
                "Xml/{controller}/{action}.xml"
            );

but whenever there is a filextension in the URL the routing fails and behaves as I was requesting a file directly.

I suspect this might be because of using extension less url handler.

<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" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Thank you for any suggestions.

Jakub

Answer

Ben Foster picture Ben Foster · Mar 4, 2014

You need to map requests for your XML files to TransferRequestHandler in web.config. Otherwise IIS will handle the request.

Jon Galloway explains how to do this here.

In summary, you add this element to location/system.webServer/handlers in your web.config:

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