0

I'm using VS2015, MVC 5 design model. Creating a link that says "View" to open a PDF file in a new browser tab. It works fine, but the document sub-directory is hard-coded in the controller. I need to pass the document sub-directory + filename to the controller. The document sub-directory is the same as the Model.id . I'm having a difficult time converting the Model.id to a string and concatenating with the filename.
The following code in the view works fine with the hard-coded sub-directory in the controller

<td>@Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = item.filename}, new { target = "_blank" })</td>

But this code does not work

<td>@Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = Convert.ToString(Model.id) + "\" + item.filename }, new { target = "_blank" })</td>

The controller action is:

    public FileResult ViewAttachedDoc(string filename)
    {
        string DocPath = ConfigurationManager.AppSettings["DocPath"];
        string path = Path.Combine(DocPath, filename);
        return File(path, "application/pdf");
    }

TIA, Tracy

TLamb
  • 121
  • 3
  • 14

1 Answers1

1

The issue is likely from trying to pass a backslash through the URL. This link of a similar question has that same problem and their solution was to use HttpUtility.UrlEncode(value); and HttpUtility.UrlDecode(value);. Otherwise if that still doesn't work, what error are you getting? P.S. C# automatically converts / -> \ for retrieving files.

Brandon
  • 51
  • 4
  • 1
    Thank you for that clue! All I did was change: @Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = Convert.ToString(Model.id) + "\" + item.filename }, new { target = "_blank" }) to: to: @Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = Convert.ToString(Model.id) + "/" + item.filename }, new { target = "_blank" }) The controller "automagically" converts the "/" to "\" – TLamb Oct 06 '22 at 21:06