I need to show a link to a file but only if the file exists. Thus far I have tried extending UrlHelper, HtmlHelper, and MvcHtmlString but none of them seem to give the results I need.
I'm sure I'm doing something wrong but I don't know what that is. The UrlHelper extension seems pretty close but the View renders the link as text instead of an anchor.
public static class UrlHelperExtensions
{
public static string Pdf(this UrlHelper helper, string fileName, string directory)
{
string _fileName = fileName + ".pdf";
string _directory = directory + "/";
string root = "~/Content/Templates/";
string path = HttpContext.Current.Server.MapPath(root + _directory + _fileName);
if (File.Exists(path))
{
return helper.Content("<a href=\"" + root + _directory + _fileName + "\" target=\"_blank\">Download Template</a>");
}
else
{
return "";
}
}
}
And then @Url.Pdf(Model.Item.Number, "Retail") gives me the text
<a href="~/Content/Templates/Retail/1001.pdf" target="_blank">Download Template</a>
on the page instead of an actual link.
