Skip to content

Lightweight, attribute-driven endpoint registration for ASP.NET Core Minimal APIs powered by a Roslyn source generator.

License

Notifications You must be signed in to change notification settings

dhindrik/TinyEndpoints

Repository files navigation

TinyEndpoints

Lightweight, attribute-driven endpoint registration for ASP.NET Core Minimal APIs powered by a Roslyn source generator.

Installation

Install the NuGet package via the .NET CLI:

dotnet add package TinyEndpoints

Or search for "TinyEndpoints" in the NuGet Package Manager in Visual Studio.

Quick Start

  1. Add a partial Program annotated with [GenerateEndpoints]:
using TinyEndpoints;

[GenerateEndpoints]
public partial class Program { }
  1. In your main Program.cs after building middleware / before app.Run() call:
RegisterGeneratedEndpoints(app);
  1. 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);
}
  1. Build & run. The generator emits RegisterGeneratedEndpoints which wires up MapGet, MapPost, etc.

Supported Attributes

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.

Configurators (Per-endpoint customization)

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.

Parameter Binding & DI

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 / ValueTask variants.

Example

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

About

Lightweight, attribute-driven endpoint registration for ASP.NET Core Minimal APIs powered by a Roslyn source generator.

Resources

License

Stars

Watchers

Forks

Languages