ErrorOrX.Generators package provides a Roslyn source generator that converts ErrorOr<T> handlers into ASP.NET Core Minimal API endpoints.
What Gets Generated
Route Attributes
Parameter Binding
The generator infers parameter sources based on HTTP method and type.Binding Priority
Service Type Detection
These patterns are detected as service types:Complex Type on GET/DELETE
Complex types on GET/DELETE require explicit binding:Interface Types with [ReturnsError]
The Problem
The Solution
Declare possible errors on the interface contract:[ReturnsError] and includes the corresponding TypedResult in the Results<...> union:
Multiple Error Types
Real-world operations have multiple failure modes. Declare them all:Middleware Attributes
The generator emits middleware fluent calls since the wrapper delegate loses original method attributes.Authorization
Multiple
[Authorize] attributes with different policies are accumulated and emitted as a single call:
[AllowAnonymous] overrides [Authorize]. When both are present, only
.AllowAnonymous() is emitted.Rate Limiting
[DisableRateLimiting] overrides [EnableRateLimiting]. When both are
present, only .DisableRateLimiting() is emitted.Output Caching
CORS
Combining Multiple Middleware
All middleware attributes can be combined on a single endpoint:JSON Context Generation
Default Behavior
WhenErrorOrGenerateJsonContext is false (default):
With Custom Context
Disable generation and use your own:MSBuild Properties
ErrorOrGenerateJsonContext is disabled by default because generated JSON
contexts cannot be processed by System.Text.Json’s source generator. See the
warning above.AOT Compatibility
The generator produces AOT-compatible code:- No
(Delegate)cast - Uses typedMapGet/MapPost - Wrapper pattern - Returns
Task, notTask<Results<...>> - Explicit
ExecuteAsync- Handles response serialization
Service Registration
The builder pattern follows ASP.NET Core conventions (likeAddRazorComponents()):
Available Methods
Calling
MapErrorOrEndpoints() without AddErrorOrEndpoints() throws an
InvalidOperationException with a clear error message.Endpoint Mapping
MapErrorOrEndpoints() returns an IEndpointConventionBuilder for global configuration:
MapRazorComponents().
API Versioning
Full API versioning support withAsp.Versioning.Http:
API Versioning Guide
Complete guide to API versioning including version formats, generated code,
service registration, and diagnostics (EOE050-EOE055).
