RSCG – MinimalApis.Discovery
name | MinimalApis.Discovery |
nuget | https://www.nuget.org/packages/MinimalApis.Discovery/ |
link | https://github.com/shawnwildermuth/MinimalApis |
author | Shawn Wildermuth |
Controller like API registering
This is how you can use MinimalApis.Discovery .
The code that you start with is
<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>
The code that you will use is
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"); } }
namespace APIDemo; public class Person { public string? FirstName { get; set; } public string? LastName { get; set; } }
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); }
The code that is generated is
// <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; } } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MinimalApis.Discovery
Leave a Reply