Skip to main content
The 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]

Without [ReturnsError], your OpenAPI spec is incomplete.When an endpoint delegates to a service method, the generator can only see the return type ErrorOr<T> - it has no idea what errors that method might return. Your Swagger UI shows only 200 OK and generic 500, while your API actually returns 404, 400, 403… Generated API clients miss error handling for real responses.

The Problem

The Solution

Declare possible errors on the interface contract:
The generator reads [ReturnsError] and includes the corresponding TypedResult in the Results<...> union:

Multiple Error Types

Real-world operations have multiple failure modes. Declare them all:
Now your OpenAPI spec is accurate: clients know shipping can fail with 404 (order doesn’t exist), 403 (not authorized), or 409 (already shipped).

Middleware Attributes

The generator emits middleware fluent calls since the wrapper delegate loses original method attributes.
This is security-critical. ASP.NET Core only sees attributes on the delegate passed to MapGet()/MapPost(). Since ErrorOrX generates a wrapper method, the original method’s attributes are invisible to ASP.NET. The generator MUST emit equivalent fluent calls.

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

Roslyn source generators cannot see output from other generators. If ErrorOrX generates a JsonSerializerContext, the System.Text.Json source generator will NOT process it, causing runtime errors in Native AOT. You MUST create your own JsonSerializerContext.

Default Behavior

When ErrorOrGenerateJsonContext is false (default):

With Custom Context

Disable generation and use your own:
The generator emits a helper file with copy-paste attributes:

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:
  1. No (Delegate) cast - Uses typed MapGet/MapPost
  2. Wrapper pattern - Returns Task, not Task<Results<...>>
  3. Explicit ExecuteAsync - Handles response serialization

Service Registration

The builder pattern follows ASP.NET Core conventions (like AddRazorComponents()):

Available Methods

Calling MapErrorOrEndpoints() without AddErrorOrEndpoints() throws an InvalidOperationException with a clear error message.

Endpoint Mapping

MapErrorOrEndpoints() returns an IEndpointConventionBuilder for global configuration:
This follows ASP.NET Core patterns like MapRazorComponents().

API Versioning

Full API versioning support with Asp.Versioning.Http:

API Versioning Guide

Complete guide to API versioning including version formats, generated code, service registration, and diagnostics (EOE050-EOE055).