title: return html file from .Net Core api
body: <p>I'm trying to use <a href="https://stackoverflow.com/a/26041298/2441637">this</a> and <a href="https://stackoverflow.com/a/221941/2441637">this</a> to get an html file as output, but got <code>error 500</code></p>
  
  <p>The objective of my app is based on the api request, the server will generate the requested output as html file, and send it to the user request.</p>
  
  <p>the codes used are:</p>
  
  <pre><code>using Microsoft.AspNetCore.Mvc;  // for Controller, [Route], [HttpPost], [FromBody], JsonResult and Json
  using System.IO;   // for MemoryStream
  using System.Net.Http; // for HttpResponseMessage
  using System.Net;  // for HttpStatusCode
  using System.Net.Http.Headers;  // for MediaTypeHeaderValue
  
  namespace server{
      [Route("api/[controller]")]
      public class FileController : Controller{
      [HttpGet]
      public HttpResponseMessage Get()
      {
          string r = @" 
              Hello There
          ";
          var stream = new MemoryStream();
          StreamWriter writer = new StreamWriter(stream);
          writer.Write(r);
          writer.Flush();
          stream.Position = 0;
  
         // processing the stream.
         byte[] Content = convert.StreamToByteArray(stream);
         var result = new HttpResponseMessage(HttpStatusCode.OK);
  
  
          result.Content.Headers.ContentDisposition =
              new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
          {
              FileName = "welcome.html"
          };
          result.Content.Headers.ContentType =
              new MediaTypeHeaderValue("application/octet-stream");
  
          return result;
      }
    }
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>using System.IO;  // for MemoryStream
  
  namespace server{
      class convert{
              public static byte[] StreamToByteArray(Stream inputStream)
              {
                  byte[] bytes = new byte[16384];
                  using (MemoryStream memoryStream = new MemoryStream())
                  {
                      int count;
                      while ((count = inputStream.Read(bytes, 0, bytes.Length)) &gt; 0)
                      {
                          memoryStream.Write(bytes, 0, count);
                      }
                      return memoryStream.ToArray();
                  }
              }
      }
  }
  </code></pre>
  
  <p>I need the returned result to be a <code>.html</code> file, so I can open it in a new browser window using JavaScript like <code>var a = window.open(returnedFile, "name");</code></p>
  
  <p><a href="https://i.stack.imgur.com/DdvFh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DdvFh.png" alt="enter image description here"></a></p>