I have middleware that hides an exception from the client and returns 500 errors in case of any exception:
public class ExceptionHandlingMiddleware { private readonly RequestDelegate _next; public ExceptionHandlingMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { try { await _next.Invoke(context); } catch (Exception exception) { var message = "Exception during processing request"; using (var writer = new StreamWriter(context.Response.Body)) { context.Response.StatusCode = 500;
My problem is that if I set the status of the response before recording the body, the client will see this status, but if I set the status after writing the message to the body, the client will receive a response with the status 200.
Can someone explain to me why this is happening?
PS I am using ASP.NET Core 1.1
source share