title: Returning a link if the file exists in asp.net mvc
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>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("&lt;a href=\"" + root + _directory + _fileName + "\" target=\"_blank\"&gt;Download Template&lt;/a&gt;");
          }
          else
          {
              return "";
          }            
      }
  }
  </code></pre>
  
  <p>And then <code>@Url.Pdf(Model.Item.Number, "Retail")</code> gives me the text </p>
  
  <pre><code>&lt;a href="~/Content/Templates/Retail/1001.pdf" target="_blank"&gt;Download Template&lt;/a&gt;
  </code></pre>
  
  <p>on the page instead of an actual link.</p>