title: Return Excel File from WebAPI ASPNetCore
body: <p>I am trying to create the WebAPI which will return an Excel file by using ClosedXML Library.</p>
  
  <p>It's all working fine, if I return the FileStreamResult.  But if I changed it to HttpResponseMessage as discussed here <a href="https://stackoverflow.com/questions/11125535/microsoft-web-api-return-a-file-use-byte">Web API Return A File</a>, I couldn't download a file or see the content anymore and getting JSON string.</p>
  
  <p>I would like to know how I could extract the File Content from this HttpResponseMessage.</p>
  
  <p>I tested in browser or PostMan tool and all I got back is JSON String:   </p>
  
  <pre><code>{
        "version": {
          "major": 1,
          "minor": 1,
          "build": -1,
          "revision": -1,
          "majorRevision": -1,
          "minorRevision": -1
        },
        "content": {
          "headers": [
            {
              "key": "Content-Disposition",
              "value": [
                "attachment; filename=ERSheet.xlsx"
              ]
            },
            {
              "key": "Content-Type",
              "value": [
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
              ]
            }
          ]
        },
        "statusCode": 200,
        "reasonPhrase": "OK",
        "headers": [],
        "requestMessage": null,
        "isSuccessStatusCode": true
      }
  </code></pre>
  
  <p>My Code is as follow:</p>
  
  <pre><code>[HttpGet("ERSheet2")]
          public HttpResponseMessage ER_GenerateWBLWorksheet2()
          {
              MemoryStream stream = new MemoryStream();
              var workbook = new XLWorkbook();
  
              var SheetNames = new List&lt;string&gt;() { "15-16", "16-17", "17-18", "18-19", "19-20" };
  
              foreach (var sheetname in SheetNames)
              {
                  var worksheet = workbook.Worksheets.Add(sheetname);
  
                  worksheet.Cell("A1").Value = sheetname;
              }
  
              workbook.SaveAs(stream);
              stream.Seek(0, SeekOrigin.Begin);
  
              HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
              result.Content = new ByteArrayContent(stream.ToArray());
              result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
              result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
              result.Content.Headers.ContentDisposition.FileName = "ERSheet.xlsx";
              return result;
          }
  </code></pre>
  
  <p><strong>Updated</strong></p>
  
  <p>This one is different from this question <a href="https://stackoverflow.com/questions/22296136">Download file with ClosedXML</a> because I mentioned that I already made it work by using 'FileStreamResult'.  I am asking this because of this comment "Better to return HttpResponseMessage with StreamContent inside of it." made in this thread <a href="https://stackoverflow.com/questions/11125535/microsoft-web-api-return-a-file-use-byte">Web API Return A File</a>.  So, trying to use HttpResponseMessage in .Net Core and didn't know that it doesn't support anymore as Darin's answer. </p>