Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make MVC accept dots in parameters? [duplicate]

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?

like image 432
Norman Avatar asked Feb 10 '15 14:02

Norman


2 Answers

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>
like image 120
AFract Avatar answered Oct 20 '22 17:10

AFract


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> ...
like image 40
Norman Avatar answered Oct 20 '22 16:10

Norman