title: IIS 8.5 File Path Not Found -- URL Encoded
body: <p>Windows Server 2012 R2</p>
  
  <p>IIS 8.5</p>
  
  <p>.NET Framework 4.5.1 installed with Windows 8.1 or Windows Server 2012 R2</p>
  
  <p>ASP .NET App written in C# in Visual Studio 2012</p>
  
  <p>This is an issue that has only recently started to occur in an application that has been running on an older operating system (I believe IIS 7.5 Windows 2008R2 but not 100% sure).</p>
  
  <p>Essentially, when the application builds a path to a file on the local system, it URL encodes the path. Then, when the application attempts to find said file, it returns File Not Found.</p>
  
  <p>The code itself is a tad silly. I didn't write it and I plan to re-write it when time permits. Here is the method that attempts to grab the file and display it on the screen:</p>
  
  <pre><code>private void OpenClaimFile(String PackageId, String ClaimId, String DocType, String AttachmentID)
  {
      string filename = string.Empty;
  
      string folderPath = Helper.PackagePath + PackageId + @"\" + ClaimId + @"\";
      string path = string.Empty;
  
  
      if (string.IsNullOrEmpty(DocType) || DocType == "&amp;nbsp;")
      {
          filename = AttachmentID;
      }
      else
      {
          filename = DocType + "_" + AttachmentID;
      }
  
      path = folderPath + filename + ".pdf";
  
      if (File.Exists(path))
      {
          Response.Clear();
          Response.AddHeader("content-disposition", "attachment;filename=" + filename);
          Response.ContentType = "application/pdf";
          Response.WriteFile(Helper.PackagePath + PackageId + "/" + ClaimId + "/" + filename + ".pdf");       
      }
      else
      {
          Alert("File Not Found!");
      }
  }
  </code></pre>
  
  <p><code>Helper.PackagePath</code> is grabbed from the web.config and is something like <code>"D:\RootPath\"</code></p>
  
  <p>I tested a file without spaces, and the file is downloaded by the browser to the Downloads folder. This is a secondary issue because it should be displayed as a PDF within the browser itself.</p>
  
  <p>Using the SysInternals ProcessMon, I found that the actual file name in the Path string is something like <code>D:\RootPath\PackageID\ClaimID\My%20Mos%20Excellent%20File.pdf</code> rather than <code>D:\RootPath\PackageID\ClaimID\My Most Excellent File.pdf.</code></p>
  
  <p>The problem appears to be that the application is now URL encoding the path that it builds and then attempting to find that file and failing. I have searched the web for any information about this and my results are next to nothing. <a href="http://programmaticallyspeaking.com/the-mysteriously-escaped-request-path-in-aspnet.html" rel="nofollow">I did find this one article</a>. </p>
  
  <p>Tonight if I get a chance, I will attempt to try the change referenced in the article I found to the web.config and see if it solves the problem. For reference, this change is:</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;appSettings&gt;
          &lt;add key="aspnet:UseLegacyRequestUrlGeneration" value="true" /&gt;
      &lt;/appSettings&gt;
  </code></pre>
  
  <p>However, I'd appreciate any insight into this issue that anyone has. </p>
  
  <p>Also, if anyone has any insight into my secondary issue, I'd appreciate that. I've only begun to look at it, but the browser should open the file in-situ and not download it. This behavior is also recent. I'm getting the idea that this is a new security setting in the system that forbids opening PDF's because they might be dangerous. I've not fully researched that issue yet, though.</p>