A super simple library to help you modularize your minimal API projects.
dotnet add package NeatApi
Looking for detail implementations in the folder /samples
. Here is the basic usage:
var builder = WebApplication.CreateBuilder(args);
+ builder.AddNeatApi();
var app = builder.Build();
+ app.MapNeatApi();
app.Run();
public class WeatherForecastServices : IServiceModule
{
public void Register(IServiceCollection services, IServiceModuleContext context)
{
services.AddSingleton<IWeatherForecastRepo, SampleWeatherForecastRepo>();
services.AddOptionsWithValidateOnStart<WeatherForecastOptions>()
.BindConfiguration(WeatherForecastOptions.Name);
}
}
public class WeatherForecastRoutes : IRoutingModule
{
protected void AddRoutes(IEndpointRouteBuilder app, IRoutingModuleContext context)
{
app.MapGet("/weatherforecast", (IWeatherForecastRepo repo) =>
{
return repo.GetForecasts();
})
.WithName("GetWeatherForecast")
.WithOpenApi();
if (context.Environment.IsDevelopment())
{
app.MapGet("debug", () =>
{
return "dotnet v" + Environment.Version.ToString();
});
}
}
}
public class WeatherForecastRoutes() : GroupedRoutingModuleBase("/weatherforecast")
{
protected override void AddRoutes(IEndpointRouteBuilder app, IEndpointConventionBuilder convention, IRoutingModuleContext context)
{
convention.WithTags("WeatherForecast").WithOpenApi();
app.MapGet("", (IWeatherForecastRepo repo) =>
{
return repo.GetForecasts();
}).WithName("GetWeatherForecast");
}
}
MIT