Skip to content

A lightweight, AOT-compatible library for organizing ASP.NET Core minimal API endpoints using source generation..

Notifications You must be signed in to change notification settings

rmja/StaticEndpoints

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

StaticEndpoints

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.

Features

  • 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

Installation

dotnet add package StaticEndpoints

Quick Start

1. Define an Endpoint

Create 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);
}

2. Register All Endpoints

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.

How It Works

  1. Compile Time: The source generator scans your assembly for all types implementing IEndpoint
  2. Code Generation: It generates a MapStaticEndpoints() extension method that calls each endpoint's AddRoute method
  3. Zero Overhead: No reflection, no runtime discovery - just direct method calls

Generated Code Example

// <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;
    }
}

Advanced Usage

Route Groups

You can use route groups to organize endpoints with common prefixes or policies:

var apiGroup = app.MapGroup("/api")
    .RequireAuthorization();

apiGroup.MapStaticEndpoints();

Requirements

  • .NET 10.0 or later
  • C# 11 or later (for static abstract interface members)

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

This project is licensed under the MIT License.

About

A lightweight, AOT-compatible library for organizing ASP.NET Core minimal API endpoints using source generation..

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages