Routing information on HttpContext not available in error handling #34855
Labels
aspnet-core/svc
fundamentals/subsvc
Source - Docs.ms
Docs Customer feedback via GitHub Issue
⌚ Not Triaged
Description
Please document that exception handlers do not receive the usual routing data in
HttpContext
- extension methods such asGetEndpoint
orGetRouteData
will always returnnull
. 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 viaHttpContext.Features
which contains the relevant information captured inExceptionHandlingMiddlewareImpl
prior to starting the exception handling process. TheIExceptionHandlerFeature
type is also shown in the very last example to access theException
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 anIExceptionHandler
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 ofGetEndpoint()
returningnull
. I have discovered theIExceptionHandlerFeature
when randomly checking theExceptionHandlingMiddlewareImpl
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
Related Issues
The text was updated successfully, but these errors were encountered: