I have this controller method in my FilesController:
public ActionResult Download(int id, string filename)
{
var file = _filesRepository.GetFile(id);
// Write it back to the client
Response.ContentType = file.FileMimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + file.FileName);
Response.BinaryWrite(file.FileData);
return new EmptyResult();
}
This works if I navigate to
/Files/Download/123?filename=myimage.png
But I’d like it work if I navigate to
/Files/Download/123/myimage.png
I know I need to create a custom route for this, but everything I’ve tried isn’t working. I’d like it to accept two parameters for only the FilesController and Download method. Is that possible?
Yes, this is very easy if you create a new route. In your
Global.asax.csfile, before the default route, add the following route:Then your controller action should work as you currently have it defined.