Link to PDF from Controller Action

Last post 05-09-2008 11:14 AM by jyoung1024. 6 replies.

Sort Posts:

  • Link to PDF from Controller Action

    05-08-2008, 5:13 PM
    • Loading...
    • jyoung1024
    • Joined on 03-09-2008, 6:10 AM
    • Posts 16

     I have a link on a page that calls an action on a controller that generates a PDF file and persists it to disk.

    Now, I want to redirect to the PDF, but the action requires a ActionResult return.

    How do I link to a PDF from a Controller action? 

  • Re: Link to PDF from Controller Action

    05-08-2008, 5:25 PM
    • Loading...
    • jyoung1024
    • Joined on 03-09-2008, 6:10 AM
    • Posts 16

    I currently have this and it seems like no redirect it happening.

                Redirect("~/GeneratedPDFs/" + fileName);
                return new EmptyResult();
     
  • Re: Link to PDF from Controller Action

    05-08-2008, 5:56 PM
    • Loading...
    • jyoung1024
    • Joined on 03-09-2008, 6:10 AM
    • Posts 16

    Got it to work with this instead of doing a redirect

     
    Response.Buffer = false; //transmitfile self buffers
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.TransmitFile("~/GeneratedPDFs/" + fileName); //transmitfile keeps entire file from loading into memory
    Response.End();
    
    return new EmptyResult();
     Still would like to know how to redirect to a PDF though.
  • Re: Link to PDF from Controller Action

    05-08-2008, 11:23 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 50

    The Redirect method returns an ActionResult.  Use return Redirect("...");

  • Re: Link to PDF from Controller Action

    05-09-2008, 9:01 AM
    • Loading...
    • jyoung1024
    • Joined on 03-09-2008, 6:10 AM
    • Posts 16

    Right but I still have to return an ActionResult.

    When I return an EmptyActionResult, after the redirect, nothing happens. 

  • Re: Link to PDF from Controller Action

    05-09-2008, 10:13 AM
    Answer
    • Loading...
    • Syc0F3ar
    • Joined on 02-03-2003, 3:13 PM
    • Posts 186

    The action result object is what tells the MVC framework what to do.  The method Redirect returns an HttpRedirectResult, which inherits from ActionResult. The MVC takes that result and executes the method ExecuteResult on that ActionResult object.  That is what is doing the redirect behind the scenes.  Now if the Redirect, or any of those methods, don't accomplish what you need to get to the PDF you generated, you can also create your own ActionResult which would handle that correctly.  You would just create a class that inherits from ActionResult and implement the ExecuteResult method to redirect, etc...  All of the methods like Redirect, RedirectToView, etc. are just creating the appropriate ActionResult for you to return.  There is nothing stopping you from creating your own ActionResult and returning it, or even instantiating any of the existing ones and returning it without using the helper methods provided on the controller.

  • Re: Link to PDF from Controller Action

    05-09-2008, 11:14 AM
    • Loading...
    • jyoung1024
    • Joined on 03-09-2008, 6:10 AM
    • Posts 16

    Ok, I'm an idiot. Tongue Tied

    I completly missed that Redirect returned an HttpRedirectResult.

    Thanks.

    Joe

Page 1 of 1 (7 items)