asp.net-mvcdownloadactionlinkfileresult

MVC - ActionLink looks for a view instead of calling of controller method


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?


Solution

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