MinApiLib.Endpoints
7.0.1
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
<PackageReference Include="MinApiLib.Endpoints" Version="7.0.1" />
paket add MinApiLib.Endpoints --version 7.0.1
#r "nuget: MinApiLib.Endpoints, 7.0.1"
// 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
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 | Versions 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. |
-
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.