MinApiLib.Endpoints 7.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package MinApiLib.Endpoints --version 7.0.1                
NuGet\Install-Package MinApiLib.Endpoints -Version 7.0.1                
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="MinApiLib.Endpoints" Version="7.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MinApiLib.Endpoints --version 7.0.1                
#r "nuget: MinApiLib.Endpoints, 7.0.1"                
#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 MinApiLib.Endpoints as a Cake Addin
#addin nuget:?package=MinApiLib.Endpoints&version=7.0.1

// Install MinApiLib.Endpoints as a Cake Tool
#tool nuget:?package=MinApiLib.Endpoints&version=7.0.1                

MinApiLib.Endpoints

license version

This package contains extensions to create endpoints using records and one file per endpoint.

Installation

You can install this package using the NuGet package manager:

Install-Package MinApiLib.Endpoints

Or using the .NET CLI:

dotnet add package MinApiLib.Endpoints

Usage

To create endpoints, you can use the MapEndpoints extension method to map all endpoints in the assembly:

global using MinApiLib.Endpoints;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapEndpoints();
app.Run();

By default, it wil map the classes in the current executing assembly, but you can change this behavior by adding the Assembly parameter:

app.MapEndpoints(typeof(SomeObject).Assembly);

Then, you can create endpoints:

public record Hello() : Get("/hello")
{
    protected override IResult Handle()
    {
        return Results.Ok("Hello World!");
    }
}

And test it with curl:

$ curl "http://localhost:5000/hello"
"Hello World!"%

This endpoint accepts asynchrony and cancellation:

public record Hello() : GetAsync("/hello")
{
    protected override async Task<IResult> HandleAsync(CancellationToken cancellationToken)
    {
        await Task.Delay(1000, cancellationToken);
        return Results.Ok("Hello World!");
    }
}

This endpoint accepts a request object:

public record struct Request(string Name);

public record Hello() : GetAsync<Request>("/hello")
{
    protected override async Task<IResult> HandleAsync(Request request, CancellationToken CancellationToken)
    {
        await Task.Delay(1000, cancellationToken);
        return Results.Ok($"Hello {request.Name}!");
    }
}

You can test it with curl:

$ curl "http://localhost:5000/hello?name=fer"
"Hello fer!"%

And you can decorate the request object with attributes:

public record struct Request([FromQuery] string Name);

This endpoint accepts also a response object:

public record struct Request(string Name, CancellationToken CancellationToken);

public record struct Response(string Message);

public record Hello() : GetAsync<Request, Response>("/hello")
{
    protected override async Task<Response> HandleAsync(Request request, CancellationToken cancellationToken)
    {
        await Task.Delay(1000, cancellationToken);
        var response = new Response($"Hello {request.Name}!");
        return response;
    }
}

You can test it with curl:

$ curl "http://localhost:5000/hello?name=fer"
{"message":"Hello fer!"}%

And you can configure any endpoint overriding the Configure method:

public record Hello() : Get("/hello")
{
    protected override RouteHandlerBuilder Configure(RouteHandlerBuilder builder)
        => builder
                .Produces(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status204NoContent)
                .WithName("Hello")
                .WithTags("hello", "world");

    protected override IResult Handle()
    {
        return Results.Ok("Hello World!");
    }
}

Here is a list of all the available endpoints:

  • Get
  • Get<TRequest>
  • Get<TRequest, TResponse>
  • GetAsync
  • GetAsync<TRequest>
  • GetAsync<TRequest, TResponse>
  • Post
  • Post<TRequest>
  • Post<TRequest, TResponse>
  • PostAsync
  • PostAsync<TRequest>
  • PostAsync<TRequest, TResponse>
  • Put
  • Put<TRequest>
  • Put<TRequest, TResponse>
  • PutAsync
  • PutAsync<TRequest>
  • PutAsync<TRequest, TResponse>
  • Delete
  • Delete<TRequest>
  • Delete<TRequest, TResponse>
  • DeleteAsync
  • DeleteAsync<TRequest>
  • DeleteAsync<TRequest, TResponse>
  • Patch
  • Patch<TRequest>
  • Patch<TRequest, TResponse>
  • PatchAsync
  • PatchAsync<TRequest>
  • PatchAsync<TRequest, TResponse>
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
9.0.0 330 11/13/2024
8.0.1 11,582 4/11/2024
8.0.0 4,333 12/14/2023
7.0.4 5,553 5/17/2023
7.0.3 428 4/18/2023
7.0.2 233 3/22/2023
7.0.1 201 3/21/2023
7.0.0 365 2/14/2023
7.0.0-beta 198 11/10/2022