Sgbj.MinimalEndpoints 1.0.0

dotnet add package Sgbj.MinimalEndpoints --version 1.0.0                
NuGet\Install-Package Sgbj.MinimalEndpoints -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sgbj.MinimalEndpoints" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sgbj.MinimalEndpoints --version 1.0.0                
#r "nuget: Sgbj.MinimalEndpoints, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sgbj.MinimalEndpoints as a Cake Addin
#addin nuget:?package=Sgbj.MinimalEndpoints&version=1.0.0

// Install Sgbj.MinimalEndpoints as a Cake Tool
#tool nuget:?package=Sgbj.MinimalEndpoints&version=1.0.0                

Sgbj.MinimalEndpoints

A simple and unopinionated way to map minimal API endpoints.

Map endpoints using the routing APIs you're already familiar with.

Installing this package

NuGet

Install-Package Sgbj.MinimalEndpoints

.NET CLI

dotnet add package Sgbj.MinimalEndpoints

Using this package

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapMinimalEndpoints(typeof(Program));

app.Run();

Choose the best pattern for your project

Lambda expression
public class HelloWorld : IMinimalEndpoint
{
    public static IEndpointConventionBuilder Map(IEndpointRouteBuilder endpoints) =>
        endpoints.MapGet("/", () => "Hello World!");
}
Static method
public class GetTodo : IMinimalEndpoint
{
    public static IEndpointConventionBuilder Map(IEndpointRouteBuilder endpoints) =>
        endpoints.MapGet("/api/todos/{id}", HandleAsync);

    public static async Task<Results<Ok<Todo>, NotFound>> HandleAsync(int id, TodoDbContext db) =>
        await db.Todos.FindAsync(id) is Todo todo ? TypedResults.Ok(todo) : TypedResults.NotFound();
}
Instance method (record or class)
public record GetTodos(TodoDbContext Db) : IMinimalEndpoint
{
    public static IEndpointConventionBuilder Map(IEndpointRouteBuilder endpoints) =>
        endpoints.MapGet("/api/todos/{id}", ([AsParameters] GetTodos handler) => handler.HandleAsync());

    public async Task<Ok<List<Todo>>> HandleAsync() => TypedResults.Ok(await Db.Todos.ToListAsync());
}
Entire APIs
public class TodoApi : IMinimalEndpoint
{
    public static IEndpointConventionBuilder Map(IEndpointRouteBuilder endpoints)
    {
        var group = endpoints.MapGroup("/api/todos");
        group.MapGet("/", GetTodosAsync);
        group.MapGet("/{id}", GetTodoAsync);
        group.MapPost("/", CreateTodoAsync);
        group.MapPut("/{id}", UpdateTodoAsync);
        group.MapDelete("/{id}", DeleteTodoAsync);
        return group;
    }

    ...
}
REPR design pattern (e.g., FastEndpoints)
public class CreateTodoEndpoint : IMinimalEndpoint
{
    public static IEndpointConventionBuilder Map(IEndpointRouteBuilder endpoints) =>
        endpoints.MapPost("/todos", HandleAsync);

    public static async Task<Results<Created<CreateTodoResponse>, ValidationProblem>> HandleAsync(
        CreateTodoRequest request, TodoDbContext db, IValidator<CreateTodoRequest> validator, IMapper mapper)
    {
        var validationResult = await validator.ValidateAsync(request);

        if (!validationResult.IsValid)
        {
            return TypedResults.ValidationProblem(validationResult.ToDictionary());
        }

        var todo = mapper.Map<Todo>(request);

        db.Todos.Add(todo);
        await db.SaveChangesAsync();

        var response = mapper.Map<CreateTodoResponse>(todo);

        return TypedResults.Created($"/api/todos/{todo.Id}", response);
    }
}

public record CreateTodoRequest(string Name);

public record CreateTodoResponse(int Id, string Name, bool IsComplete);

public class CreateTodoValidator : AbstractValidator<CreateTodoRequest>
{
    public CreateTodoValidator()
    {
        RuleFor(x => x.Name).NotEmpty();
    }
}

public class CreateTodoProfile : Profile
{
    public CreateTodoProfile()
    {
        CreateMap<CreateTodoRequest, Todo>();
        CreateMap<Todo, CreateTodoResponse>();
    }
}
MediatR
public class DeleteTodoEndpoint : IMinimalEndpoint
{
    public static IEndpointConventionBuilder Map(IEndpointRouteBuilder endpoints) =>
        endpoints.MapDelete("/api/todos/{id}", ([AsParameters] DeleteTodoRequest request, IMediator mediator) => mediator.Send(request));
}

public record DeleteTodoRequest(int Id) : IRequest<IResult>;

public class DeleteTodoHandler : IRequestHandler<DeleteTodoRequest, IResult>
{
    ...
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 605 8/17/2022