Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing information on HttpContext not available in error handling #34855

Open
zdenek-jelinek opened this issue Feb 28, 2025 · 1 comment
Open

Comments

@zdenek-jelinek
Copy link

zdenek-jelinek commented Feb 28, 2025

Description

Please document that exception handlers do not receive the usual routing data in HttpContext - extension methods such as GetEndpoint or GetRouteData will always return null. Ideally, if you could also add an official reason for this - my guess is that the error handling process may change these values so this might make sense to avoid inconsistencies and surprises.

Instead of using GetEndpoints and similar, IExceptionHandlerFeature can be accessed via HttpContext.Features which contains the relevant information captured in ExceptionHandlingMiddlewareImpl prior to starting the exception handling process. The IExceptionHandlerFeature type is also shown in the very last example to access the Exception but no further information is given.


Background:
I am migrating an older API with sub-optimal error response payloads to use ProblemDetails for new API endpoints. I'd like to choose an IExceptionHandler based on action descriptor. I'm very happy with the handler checking the descriptor and if it finds it's not relevant, return false and let the next one have a go. Unfortunately, I have hit the issue of GetEndpoint() returning null. I have discovered the IExceptionHandlerFeature when randomly checking the ExceptionHandlingMiddlewareImpl source code.

There is another issue in aspnetcore proper, dotnet/aspnetcore#54192, where using IExceptionHandlerFeature is also recommended as a workaround.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/error-handling.md

Document ID

38515dfb-91a5-b395-db9d-084bbaf095c8

Article author

@tdykstra

Metadata

  • ID: 0e03fdf4-b19b-17c3-04f4-336ab6d2ee06
  • PlatformId: c1f85027-4da3-44d1-3171-d49c58a44c78
  • Service: aspnet-core
  • Sub-service: fundamentals

Related Issues

@zdenek-jelinek
Copy link
Author

zdenek-jelinek commented Feb 28, 2025

There's one more consequence worth noting: When using IProblemDetailsService from an IExceptionHandler, the AdditionalMetadata must be explicitly populated with the endpoint metadata from the exception handler feature.

Otherwise, the configured API ProblemDetailsWriter will not be used as it checks for endpoint metadata which is not on the HttpContext.

The middleware does this but a mere user like me has no way to know this.

This explains some of the issues I had with my previous project! There, the problem details configuration was not respected and I ended up calling ProblemDetailsFactory explicitly as a workaround instead. I just couldn't figure out why and gave up.

This would also mean that the last code example at the page in question might not be entirely correct - at least not as a general showcase of how to use IProblemDetailsService from an error handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant