MinimalApis.Discovery by Shawn Wildermuth
Nuget / site data
Details
Info
Name: MinimalApis.Discovery
A utility library to allow for structuring Minimal APIs and registering them via source generator.
Author: Shawn Wildermuth
NuGet: https://www.nuget.org/packages/MinimalApis.Discovery/
You can find more details at https://github.com/shawnwildermuth/MinimalApis
Original Readme
Minimal API Tools
This is the home of a set of tools that I've created for Minimal APIs. These include:
- MinimalApis.Discovery - A tool for organizing your Minimal APIs
- MinimalApis.FluentValidation - Endpoint filter for more easily tying Validators to specific Minimal API Endpoints.
About
Controller like API registering
How to use
Example ( source csproj, source files )
- CSharp Project
- PersonAPI.cs
- Person.cs
- Program.cs
This is the CSharp Project that references MinimalApis.Discovery
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="MinimalApis.Discovery" Version="1.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of MinimalApis.Discovery in PersonAPI.cs
using Microsoft.AspNetCore.Http.HttpResults;
using MinimalApis.Discovery;
namespace APIDemo;
public class PersonAPI : IApi
{
public void Register(IEndpointRouteBuilder builder)
{
var grp = builder.MapGroup("/api/Person");
grp.MapGet("", GetFromId);
grp.MapGet("{id:int}", GetFromId);
//todo: add more routes
}
public static async Task<Person[]> GetAll()
{
await Task.Delay(1000);
return new[] { new Person { FirstName = "Ignat", LastName = "Andrei" } };
}
public static async Task<Results<Ok<Person>,NotFound<string>>> GetFromId(int id)
{
await Task.Delay(1000);
if (id == 1)
{
return TypedResults.Ok<Person>(new Person { FirstName = "Ignat", LastName = "Andrei" });
}
return TypedResults.NotFound<string>("Person not found");
}
}
This is the use of MinimalApis.Discovery in Person.cs
namespace APIDemo;
public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
This is the use of MinimalApis.Discovery in Program.cs
using MinimalApis.Discovery;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.MapApis();
app.Run();
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- MinimalApiDiscovery.g.cs
// <auto-generated/>
using System;
using Microsoft.AspNetCore.Builder;
namespace MinimalApis.Discovery
{
public static class MinimalApisDiscoveryGeneratedExtensions
{
public static WebApplication MapApis(this WebApplication app)
{
// Call Register on all classes that implement IApi
new global::APIDemo.PersonAPI().Register(app);
return app;
}
}
}
Usefull
Download Example (.NET C# )
Share MinimalApis.Discovery
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MinimalApis.Discovery