Create a minimal endpoint from any function in any file.
Genesis.MinimalApis provides support for easily exposing minimal endpoints from any file.
Important!
MapPatch
andHttpPatchAttribute
are only supported in dotnet 7.0 and greater! UseMapPost
andHttpPostAttribute
in dotnet 6.0!Please reference MapPatch
Add the following package from nuget
dotnet add package Genesis.MinimalApis
Any function in pretty much any file can be exposed as a minimal endpoint via adding the IEndpoints
interface to a class or struct or by adding any HttpMethodAttribute
found in Microsoft.AspNetCore.Mvc
.
Full DI support is provided for endpoints registered either manually or by the use of attributes. Additional examples are provided under Samples.
Endpoints can be registered under any lifecycle.
If registering a class that inherits from IEndpoints
helper dependency injection functions for services are provided.
AddTransientEndpoints<T>(this IServiceCollection services) where T : IEndpoint
AddScopedEndpoints<T>(this IServiceCollection services) where T : IEndpoint
AddSingletonEndpoints<T>(this IServiceCollection services) where T : IEndpoint
Structs that inherit from IEndpoints
may also be registered, though they require a but more setup.
RegisterEndpoints<T>(this IServiceCollection services, Func<ServiceProvider, object> func);
Static classes can be registered when using attributes found in Microsoft.AspNetCore.Mvc
.
Endpoints can be defined and used in class files by using the IEndpoints
interface.
//. GreetingService.cs
internal sealed class GreetingService : IEndpoints {
//. function to expose as an endpoint
public string Greet(string name) =>
$"Hello {name}!";
//. function from interface to which will be used to register the app.
public void RegisterEndpoints(IEndpointRouteBuilder app) {
app.MapGet("greet/{name}", Greet)
};
}
//. Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingletonEndpoints<GreetingService>();
var app = builder.Build();
//. Register endpoint GET /greet/{name} in GreetingService.cs
app.MapEndpoints<GreetingService>();
app.Run();
Endpoints can be exposed by using attributes found in Microsoft.AspNetCore.Mvc
and Microsoft.AspNetCore.Mvc.Routing
.
Supported Attributes:
//. GreetingService.cs
using Microsoft.AspNetCore.Mvc;
[Route("api")]
internal sealed class GreetingService {
[HttpGet("greet/{name}")]
public string Greet(string name) =>
$"Hello {name}!";
}
//. Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<GreetingService>();
var app = builder.Build();
//. Map attributes in GreetingService to GET /api/Greet/{name}
app.MapEndpoints<GreetingService>();
app.Run();
Attributes can be used on static classes such that static functions can be registered as endpoint.
Only method injection will work using this method of registration as static classes cannot be instantiated.
//. GreetingFunctions.cs
using Microsoft.AspNetCore.Mvc;
[Route("api")]
internal static class GreetingFunctions {
[HttpGet("greet/{name}")]
public static string Greet(string name) =>
$"Hello {name}!";
}
//. Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
//. Map attributes in GreetingFunctions to GET /api/Greet/{name}
app.MapStaticEndpoints<GreetingService>();
app.Run();