Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

17 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:

publicinterfaceIEndpoint{staticabstractvoidAddRoute(IEndpointRouteBuilderbuilder);}

That is 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
  • Endpoint Groups: Scope generated registration methods to named groups for fine-grained control

Installation

dotnet add package StaticEndpoints

Quick Start

1. Define an Endpoint

usingStaticEndpoints;namespaceMyApp.Features.WeatherForecast;publicclassGetWeatherForecast:IEndpoint{publicstaticvoidAddRoute(IEndpointRouteBuilderbuilder){builder.MapGet("/weatherforecast",HandleAsync).WithName("GetWeatherForecast");}staticWeatherForecast[]HandleAsync(){varsummaries=new[]{"Freezing","Bracing","Chilly","Cool","Mild"};returnEnumerable.Range(1,5).Select(index =>newWeatherForecast(DateOnly.FromDateTime(DateTime.Now.AddDays(index)),Random.Shared.Next(-20,55),summaries[Random.Shared.Next(summaries.Length)])).ToArray();}recordWeatherForecast(DateOnlyDate,intTemperatureC,string?Summary);}

2. Register All Endpoints

app.MapStaticEndpoints();// Auto-discovers and registers all endpoints

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 AddRoute method
  3. Zero Overhead: No reflection, no runtime discovery - just direct method calls

Advanced Usage

Route Groups

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

Endpoint Groups

Endpoint groups let you split your endpoints across multiple generated MapStatic{Group}Endpoints() methods so that different sections of your API can be mounted or secured independently.

  • Endpoints with no [EndpointGroup] attribute, or tagged with [EndpointGroup(null)], are included in the default MapStaticEndpoints() method.
  • Endpoints can belong to multiple groups by stacking attributes.

Option 1 - Per-class attribute

[EndpointGroup("Admin")]publicclassDeleteUser:IEndpoint{ ...}// Belongs to both Admin and Public groups[EndpointGroup("Admin")][EndpointGroup("Public")]publicclassGetUsers:IEndpoint{ ...}

Option 2 - Assembly-level attribute (namespace prefix)

Declare group mappings once in Program.cs or AssemblyInfo.cs using the Namespace property:

[assembly:EndpointGroup("Admin",Namespace="MyApp.Features.Admin")][assembly:EndpointGroup("Public",Namespace="MyApp.Features.Public")]

All endpoints whose namespace starts with the given prefix are automatically assigned to the group - no per-class attribute needed.

Note: If an endpoint class carries any class-level [EndpointGroup] attribute, assembly-level mappings are not applied to that class, giving you precise per-class overrides where needed.

Option 3 - Combine both

Use assembly-level attributes for the common case and class-level attributes for exceptions or multi-group membership.

Using the generated group methods

app.MapStaticEndpoints();// ungrouped endpointsapp.MapGroup("/admin").RequireAuthorization().MapStaticAdminEndpoints();// endpoints in the "Admin" groupapp.MapGroup("/public").MapStaticPublicEndpoints();// endpoints in the "Public" group

Generated output:

// <auto-generated/>publicstaticpartialclassStaticEndpointsBuilderExtensions{publicstaticIEndpointRouteBuilderMapStaticEndpoints(...){ ...}// ungroupedpublicstaticIEndpointRouteBuilderMapStaticAdminEndpoints(...){ ...}// "Admin" grouppublicstaticIEndpointRouteBuilderMapStaticPublicEndpoints(...){ ...}// "Public" group}

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

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages