Middlewares
ASP.NET Core middleware for cross-cutting concerns.
ExceptionHandlingMiddleware
Global exception handling with RFC 7807 Problem Details.
Features
- Catches unhandled exceptions
- Returns consistent error responses
- Logs exceptions with trace ID
- Hides sensitive info in production
Exception Mapping
| Exception | Status Code |
|---|---|
| ValidationException | 400 Bad Request |
| UnauthorizedAccessException | 401 Unauthorized |
| KeyNotFoundException | 404 Not Found |
| ArgumentException | 400 Bad Request |
| InvalidOperationException | 409 Conflict |
| OperationCanceledException | 499 Client Closed |
| Others | 500 Internal Server Error |
CorrelationIdMiddleware
Adds correlation IDs for request tracing.
Headers
- X-Correlation-ID
- X-Request-ID
Features
- Reads existing ID from request headers
- Generates new ID if not present
- Adds ID to response headers
- Sets HttpContext.TraceIdentifier
- Adds to logging scope
Registration
csharp
// Individual
app.UseMiddleware<CorrelationIdMiddleware>();
app.UseMiddleware<ExceptionHandlingMiddleware>();// Or use extension app.UseGryd(app.Environment);