dotnet / dotnet/AspNetCore.Docs

Routing information on HttpContext not available in error handling

Open
#34,855 1 comment 0 reactions 1 assignee Claimed by @tdykstra View on GitHub
aspnet-core/svc fundamentals/subsvc re-Aditya Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

### 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, https://github.com/dotnet/aspnetcore/issues/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](https://github.com/dotnet/AspNetCore.Docs/issues?q=is%3Aissue+is%3Aopen+38515dfb-91a5-b395-db9d-084bbaf095c8)

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.