0

I would like to create link on my site, which, after click, would open download window (just some simple text file). In several tutorials I found a way to do it, however, for some reason, it seems that ActionLink doesnt call my method and looks for a view instead

My ActionLink

@Html.ActionLink("here is the gpx log", "Download", "Treks")

My Download method in Treks controller (added also following method using attribute routing in case it the case of the mess)

public FileResult Download()
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(@"~/Files/file.txt");
    string fileName = "file.txt"; //I will add parameters later, once the basics work
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}


[Route("treks/{trekname}")] //Route: /Users/12
public ActionResult ShowTrek(string trekname) 
{
    return View(trekname);
}

And this is the error I always get

The view 'Download' or its master was not found or no view engine supports the searched locations. The following locations were searched..

~/Views/Treks/DownloadFiles.aspx blahblahbla:

I spent one hour working on this and still not an inch closer to the solution. Does anybody know where I am making a mistake? Thanks a lot

Update: This is the content of my RouteConfig file

public static void RegisterRoutes(RouteCollection routes)
       {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
       routes.MapMvcAttributeRoutes();

       routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );  
   }

Edit: Ok, I debugged it. Seems the problem is in attribute routing. For some reason, controller ignored Download method and goes directly for ActionResult ShowTrek... any idea how to fix it?

Community
  • 1
  • 1
Jozef
  • 479
  • 1
  • 9
  • 36
  • I just create a simple MVC application, do something like you and it's works fine(can route to Download and response a file). Try to check your routing config. – Brian Sep 26 '16 at 07:10
  • Hi, I dont remember deleting some stuff from my routing config, only change I made there was that I enabled Attribute routing (I use attribute routing in the controller, but only after Download method). I added content of my RouteConfig file into the question. – Jozef Sep 26 '16 at 07:18
  • Your `ShowTrek` method would throw an exception because its would be trying to find a view with the name equal to the parameter `trekname` (which I assume is "Download"). You need to use `return View((object)trekname);` or better return a model with a property for `trekname` –  Sep 26 '16 at 07:31
  • Thanks, I will do it - problem was truly with attribute routing. Or move Download function to another controller.. – Jozef Sep 26 '16 at 07:47

2 Answers2

0

Try to replace Fileresult with FileStreamResult you may also need to create filestream object inside your method

new FileStream(fileName, FileMode.Open)

public FileStreamResult Download()
{
 // Your code

}
Tassadaque
  • 8,129
  • 13
  • 57
  • 89
0

Solved. Problem was in attribute routing. Pls see answer of Stephen Muecke in comments

Jozef
  • 479
  • 1
  • 9
  • 36