title: How to avoid duplicate content-disposition headers with MVC3 FileContentResult?
body: <p>We have some files stored in sql database. On an ASP.NET MVC3 form, we display 2 links:</p>
  
  <p>View this file | Download this file</p>
  
  <p>These links go to these corresponding action methods. The download works as expected -- clicking a link forces a save dialog in the browser. However, the display is causing duplicate Content-Disposition headers to be sent to the browser, resulting in an error on Chrome, and an empty page in Firefox.</p>
  
  <pre><code>[ActionName("display-file")]
  public virtual ActionResult DisplayFile (Guid fileId, string fileName)
  {
      var file = _repos.GetFileInfo(fileId);
      if (file != null)
      {
          Response.AddHeader("Content-Disposition", 
              string.Format("inline; filename={0}", file.Name));
          return File(file.Content, file.MimeType, file.Name);
      }
  }
  
  [ActionName("download-file")]
  public virtual ActionResult DownloadFile (Guid fileId, string fileName)
  {
      var file = _repos.GetFileInfo(fileId);
      if (file != null)
      {
          return File(file.Content, file.MimeType, file.Name);
      }
  }
  </code></pre>
  
  <p>Here are the 2 headers sent to the browser for the display action:</p>
  
  <pre><code>Content-Disposition: inline; filename=name-of-my-file.pdf
  Content-Disposition: attachment; filename="name-of-my-file.pdf"
  </code></pre>
  
  <p>I tried changing my custom content-disposition header to wrap the file name in double quotes, but it still sent 2 headers to the browser. I also tried removing the Content-Disposition header before adding the custom one, but it appears the attachment header is being added after the FileContentResult is returned. </p>
  
  <p>This code used to work. I ran a test just yesterday and noticed it is no longer working in Chrome or Firefox. This could be due to updates in the browsers. IE8 and Safari still open the file correctly. </p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks again Darin, you are correct. We actually used this approach because of <a href="https://stackoverflow.com/q/3206682/304832">another question you answered</a>. </p>
  
  <p>A little more info about how this was ultimately solved on our end, we have a custom route for the display file link:</p>
  
  <pre><code>context.MapRoute(null,
      "path/to/display-file-attachment/{fileId}/{fileName}",
      new
      {
          area = "AreaName",
          controller = "ControllerName",
          action = "DisplayFile",
      }
  );
  </code></pre>
  
  <p>The hyperlink on the page passes the file name to the action method through the route parameter, so it is already part of the URL. Thus, we did not need to add a custom content-disposition header in order to make the file name match the system's when a user decided to download it (by clicking save icon in browser PDF viewer). So we just used this:</p>
  
  <pre><code>[ActionName("display-file")]
  public virtual ActionResult DisplayFile (Guid fileId, string fileName)
  {
      var file = _repos.GetFileInfo(fileId);
      if (file != null)
      {
          // no custom content-disposition header, and no 3rd fileName argument
          return File(file.Content, file.MimeType);
      }
  }
  </code></pre>