dotnet / dotnet/aspnetcore

[AspNetCore .NET 9] Headers lost when HttpContext is cleared in ExceptionHandlerMiddlewareImpl

Open
#63,187 0 comments 2 reactions 0 assignees View on GitHub
area-middleware
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

When using the new .NET 7 ProblemDetails-based exception handling, the middleware pipeline is not reexecuted during ExceptionHandler execution in [ExceptionHandlerMiddlewareImpl](https://github.com/dotnet/aspnetcore/blob/9fd58cfcfc83d92bf372459e62ab4b017cc8da7b/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs).

This leads to the clearing of headers that were set during the initial middleware execution,
after problem details are returned in [DefaultProblemDetailsWriter](https://github.com/dotnet/aspnetcore/blob/52b1c18e83dab1fae63b38bd4ed9538634ca3ded/src/Http/Http.Extensions/src/DefaultProblemDetailsWriter.cs#L17).

Have I misconfigured my ASP.NET application / is there a correct way of reexecuting middleware when returning ProblemDetails using a custom IExceptionHandler?

Reproducible example: https://github.com/daniel-dewa/ErrorDetailsExample

### Expected Behavior

I expect middleware to be reexecuted or not be cleared when returning an exception.

### .NET Version

9.0.108

### Anything else?

ASP.NET Core version: `Microsoft.AspNetCore.App 9.0.7`
Might have something to do with #51888?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.