title: ASP.NET Core HttpContext.Response.StatusCode is 200 when exception
body: <p>I have an endpoint that throws exception.</p>
  <pre><code>[ApiController]
  [Route(&quot;api&quot;)]
  public class LegacyController : ControllerBase
  {
      [HttpPost(&quot;endpoint&quot;)]
      public async Task&lt;ActionResult&lt;IEnumerable&lt;Entitty&gt;&gt;&gt; GetAll()
      {
          throw new Exception();
          return Ok(t.ToList());
      }
  }
  </code></pre>
  <p>I have custom Http logging filter that looks like this</p>
  <pre><code>using Microsoft.AspNetCore.Http;
  
  namespace Client.API.Extensions
  {
      public class HttpLoggingMiddleware
      {
          private readonly RequestDelegate _next;
          private readonly ILogger _logger;
  
          public HttpLoggingMiddleware(RequestDelegate next, ILogger&lt;HttpLoggingMiddleware&gt; logger)
          {
              _next = next;
              _logger = logger;
          }
  
          public async Task Invoke(HttpContext context)
          {
              try
              {
                  await _next(context);
              }
              finally
              {
                  _logger.LogInformation(
                      &quot;Request {method} {url} =&gt; {statusCode}&quot;,
                      context.Request?.Method,
                      context.Request?.Path.Value,
                      context.Response?.StatusCode);
              }
          }
      }
  }
  </code></pre>
  <p>When I'm calling this endpoint, context.Response?.StatusCode is 200. Why?</p>
  <p>However, Postman shows me that my request failed, I see en error itself, therefore everything looks normal. Although logs contain 200 status code message, not the 500.</p>
  <p>Edit: here is my startup (some parts are removed).</p>
  <pre><code>namespace Client.API
  {
      public class Startup
      {
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              ...
              app.UseHttpsRedirection();
  
              app.UsePathBase(new PathString(basePath));
              app.UseRouting();
  
              app.UseMiddleware&lt;HttpLoggingMiddleware&gt;();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
              endpoints.MapControllers();
              });
          }
      }
  }
  </code></pre>