A lightweight, AOT-compatible library for organizing ASP.NET Core minimal API endpoints using source generation.
The library consists of a single, simple interface:
public interface IEndpoint
{
static abstract void AddRoute(IEndpointRouteBuilder builder);
}That's it. Implement this interface on your endpoint classes, and the source generator handles the rest.
- ✨ AOT-Compatible: Fully compatible with Native AOT compilation
- 🚀 Source Generator: Zero runtime reflection - all endpoint registration is generated at compile time
- 🎯 Type-Safe: Leverage C# static abstract interface members for compile-time safety
- 📦 Minimal Dependencies: Only depends on ASP.NET Core framework
- 🏗️ Clean Architecture: Organize endpoints alongside your features with vertical slice architecture
dotnet add package StaticEndpointsCreate a class that implements IEndpoint with a static AddRoute method:
using StaticEndpoints;
namespace MyApp.Features.WeatherForecast;
public class GetWeatherForecast : IEndpoint
{
public static void AddRoute(IEndpointRouteBuilder builder)
{
builder.MapGet("/weatherforecast", HandleAsync)
.WithName("GetWeatherForecast");
}
static WeatherForecast[] HandleAsync()
{
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild" };
return Enumerable.Range(1, 5)
.Select(index => new WeatherForecast(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
}
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary);
}In your Program.cs, call the auto-generated MapStaticEndpoints() method:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.MapStaticEndpoints(); // ✨ Auto-discovers and registers all endpoints
app.Run();That's it! The source generator automatically finds all IEndpoint implementations and generates the registration code.
- Compile Time: The source generator scans your assembly for all types implementing
IEndpoint - Code Generation: It generates a
MapStaticEndpoints()extension method that calls each endpoint'sAddRoutemethod - Zero Overhead: No reflection, no runtime discovery - just direct method calls
// <auto-generated/>
public static partial class StaticEndpointsBuilderExtensions
{
public static IEndpointRouteBuilder MapStaticEndpoints(
this IEndpointRouteBuilder app,
RouteGroupBuilder? routeGroupBuilder = null)
{
var builder = routeGroupBuilder ?? app;
MyApp.Features.WeatherForecast.GetWeatherForecast.AddRoute(builder);
// ... other endpoints
return app;
}
}You can use route groups to organize endpoints with common prefixes or policies:
var apiGroup = app.MapGroup("/api")
.RequireAuthorization();
apiGroup.MapStaticEndpoints();- .NET 10.0 or later
- C# 11 or later (for static abstract interface members)
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is licensed under the MIT License.