Shared : Presentation Minimal API
The Shared.Presentation.MinimalApi project provides the presentation layer abstractions and utilities for ASP.NET Core Minimal API applications. It includes an endpoint interface, dependency injection helpers for service and endpoint registration, CORS configuration, global exception handling, and a Result-to-ProblemDetails conversion extension.
Abstractions : Endpoints
The IEndpoint interface defines a single AddRoutes method that endpoints implement to register their routes on the IEndpointRouteBuilder. This provides a modular pattern for organizing Minimal API endpoints.
Dependency Injection
- WebApplicationBuilderExtension : Provides
AddMinimalApito register CORS (with validated allowed origins), problem details, a global exception handler, and OpenAPI with anAccept-Languageheader parameter on all operations. Also providesRegisterEndpointsandAddEndpointsto discover and register allIEndpointimplementations from a given assembly. - WebApplicationExtension : Provides
UseExceptionHandlingto enable the global exception handler middleware,UseMinimalApito configure CORS, OpenAPI and Scalar API reference (in development), HSTS, and HTTPS redirection, andUseEndpointsto resolve all registeredIEndpointservices and map their routes.
Extensions : Results
The ToProblemDetailsExtension class provides an extension method that converts a read-only list of Error objects into an IResult containing a ProblemDetailsDto. It maps the first non-internal error type to the appropriate HTTP status code, title, and RFC link (400 Bad Request, 404 Not Found, 422 Unprocessable Entity, or 500 Internal Server Error), and returns the response as application/problem+json.
Handlers : Error Manager
The ErrorManagerHandler is a sealed IExceptionHandler implementation that provides global exception handling for the Minimal API. It logs OperationCanceledException as warnings and returns a 499 status code, while all other exceptions are logged as errors and returned as a ProblemDetailsDto JSON response with a 500 status code. In debug mode, the full exception details are included; in release mode, a generic "Unknown error" message is returned.
Options : CORS
- CorsOption : A sealed options class that holds the array of allowed origins, bound from the
Corsconfiguration section. It also defines the section name and the CORS policy name as constants. - CorsOptionValidator : A FluentValidation validator that ensures the
AllowedOriginsarray is not empty.