I started by making an UrlHelperExtension class that built my url likes this
"/Home/DownloadFiles?directory="+directory+"&filename"+HttpUtility.UrlEncode(filename)
But i figure why not use mvc to build my URLs so in my view i did
href="@Url.Action("DownloadFiles","Home", new {directory = "files", filename="1.mp3"}"
but that's outputting /Home/DownloadFiles/files/1.mp3 which can't find the file (i get a 404). my action method is
public ActionResult DownloadFiles(string directory, string filename){
//log which file is downloaded by who
//Add download header content-disposition attachement
//Send response with the file
return null;
}
and my only route looks like this
routes.MapRoute(
name:"Default",
url: "{controller}/{action}/{directory}/{filename}",
defaults: new {controller = "Home", action = "Index", directory = UrlParameter.Optional, filename = UrlParameter.Optional}
)
I think i have some issues really understanding routes because i'm not sure how to fix this so i dont have to use my extension class which really doesn't do much. Maybe i shouldn't use URL.Action ? Would Url.Action UrlEncode the filename parameter ? the directory parameter is only 1 "deep" so it can't be abc/def only abc and i add to it relevant part so im not worried about UrlEncoding it.
You're getting a 404, not because you have an issue with your routes (they look fine), but because the path /Home/DownloadFiles/files/282.mp3
is being processed as a static file, which of course does not exist.
Try adding the following to your web.config
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Update
If performance is an issue. Add this to your web.config
instead:
<system.webServer>
<handlers>
<add name="Mp3FileHandler" path="*.mp3" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>