title: The response headers cannot be modified because the response has already started
body: <p>I am using Asp.Net core 3.1</p>
  <p>I have implemented a functionality to download Excel/CSV file by writing data to HttpContext Response and I am able to download the file but I get error log saying</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>[ERR] An unhandled exception has occurred while executing the request. (48a46595)
  System.InvalidOperationException: The response headers cannot be modified because the response has already started.</code></pre>
  </div>
  </div>
  </p>
  <p>Also I deployed my application on the server and get error code 502.</p>
  <p>Below is the code.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public static class IWorkBookExtensions
      {
          public static void WriteExcelToResponse(this IWorkbook book, HttpContext httpContext, string templateName, ILogger&lt;ExportDataController&gt; _logger)
          {
              try
              {
                  var response = httpContext.Response;
                  response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                  if (!string.IsNullOrEmpty(templateName))
                  {
                      var contentDisposition = new Microsoft.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                      contentDisposition.SetHttpFileName(templateName);
                      response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
                  }
                  book.Write(response.Body);
                  
              }
              catch(Exception ex)
              {
                  _logger.LogError(ex.InnerException.ToString() + ex.Message);
              }
             
          }
       }</code></pre>
  </div>
  </div>
  </p>
  <p>Below is the error log</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>2020-10-13T14:45:17.1452752-05:00 80003b-00-fc00-3f-84967bb [ERR] An unhandled exception has occurred while executing the request. (48a46595)
  System.InvalidOperationException: The response headers cannot be modified because the response has already started.
     at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
     at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
     at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_ContentType(String value)
     --- End of stack trace from previous location where exception was thrown ---
     2020-10-13T14:45:17.1546662-05:00 80003b-00-fc00-3f-84967bb [WRN] The response has already started, the error page middleware will not be executed. (e1dd4c0d)
  2020-10-13T14:45:17.2073595-05:00 880003b-00-fc00-3f-84967bb [ERR] Connection ID ""18300129210426"", Request ID ""80003b-00-fc00-3f-84967bb"": An unhandled exception was thrown by the application. (bf3147)
  System.InvalidOperationException: The response headers cannot be modified because the response has already started.</code></pre>
  </div>
  </div>
  </p>