8

I have following route config:

routes.MapRoute(
            name: "Downloads",
            url: "downloads/{filename}",
            defaults: new { controller = "Downloads", action = "Index", filename = UrlParameter.Optional }
        );

and following controller code:

public ActionResult Index(string filename)
    { ...

When I call this Action with http://test.com/downloads/test.txt I get a 404. When I call the Action without dots in the filename it works. How can I make MVC pass full filenames to my parameter filename?

Norman
  • 3,279
  • 3
  • 25
  • 42

2 Answers2

8

This happens probably because if your url contains a dot, IIs handles it as a "physical path" url rather a rewritten one.

One neat way to overcome this is to define a handler for you own scheme, for example :

<system.webServer>    
  <handlers>      
    <add name="ApiURIs-ISAPI-Integrated-4.0"
        path="/downloads/*"
        verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
        type="System.Web.Handlers.TransferRequestHandler"
        preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>
AFract
  • 8,868
  • 6
  • 48
  • 70
1

Thanks @Coder. I added the following code (Dots in URL causes 404 with ASP.NET mvc and IIS) to my web.config and it worked:

<system.webServer>
<handlers>
  <add name="ApiURIs-ISAPI-Integrated-4.0" path="/downloads/*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers> ...
Community
  • 1
  • 1
Norman
  • 3,279
  • 3
  • 25
  • 42