Skip to content

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

ExceptionStatus Code
ValidationException400 Bad Request
UnauthorizedAccessException401 Unauthorized
KeyNotFoundException404 Not Found
ArgumentException400 Bad Request
InvalidOperationException409 Conflict
OperationCanceledException499 Client Closed
Others500 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);

Released under the MIT License.