Lightweight, attribute-driven endpoint registration for ASP.NET Core Minimal APIs powered by a Roslyn source generator.
Install the NuGet package via the .NET CLI:
dotnet add package TinyEndpointsOr search for "TinyEndpoints" in the NuGet Package Manager in Visual Studio.
- Add a partial
Programannotated with[GenerateEndpoints]:
using TinyEndpoints;
[GenerateEndpoints]
public partial class Program { }- In your main
Program.csafter building middleware / beforeapp.Run()call:
RegisterGeneratedEndpoints(app);- Create an endpoints class with methods (static or instance) decorated with verb attributes:
using TinyEndpoints;
public class UserEndpoints
{
[Get("users/{id}")]
public IResult GetUser(IUserService svc, string id) =>
svc.TryGet(id, out var u) ? Results.Ok(u) : Results.NotFound();
[Post<AuthConfigurator>("member/users")]
public IResult CreateUser(IUserService svc, CreateUser dto) =>
Results.Created($"/users/{dto.Id}", dto);
}- Build & run. The generator emits
RegisterGeneratedEndpointswhich wires upMapGet,MapPost, etc.
GetAttribute
GetAttribute<TConfigurator>
PostAttribute
PostAttribute<TConfigurator>
PutAttribute
PutAttribute<TConfigurator>
DeleteAttribute
DeleteAttribute<TConfigurator>Each constructor takes a single route string. Generic form auto-assigns the configurator type; non-generic form can set ConfiguratorType = typeof(MyConfigurator) optionally.
Implement IEndpointConfigurator:
public class AuthConfigurator : IEndpointConfigurator
{
public void Configure(RouteHandlerBuilder builder)
=> builder.RequireAuthorization();
}Use either style:
[Get<AuthConfigurator>("member/profile")]
// or
[Get("member/profile", ConfiguratorType = typeof(AuthConfigurator))]Inside Configure you can call any RouteHandlerBuilder methods: RequireAuthorization, AddEndpointFilter, WithName, WithTags, Produces, etc.
Matches standard Minimal APIs:
- Constructor parameters of handlers are resolved from DI by type.
- Route parameters bound by name.
- Complex body parameters for POST/PUT.
- Source-specific attributes like
[FromHeader],[FromQuery], etc. honored. - Return types:
IResult, POCO/value,Task/ValueTaskvariants.
public class ProductEndpoints
{
[Get("products/{id}")]
public static IResult GetById(IProductService products, string id)
=> products.GetByIdOrNull(id) is { } p ? Results.Ok(p) : Results.NotFound();
[Post<AuthConfigurator>("member/products")]
public IResult Create(IProductService products, CreateProduct dto)
=> Results.Created($"/products/{dto.Id}", products.Add(dto));
}