RSCG – MinimalHelpers.Routing.Analyzers
RSCG – MinimalHelpers.Routing.Analyzers
name | MinimalHelpers.Routing.Analyzers |
nuget | https://www.nuget.org/packages/MinimalHelpers.Routing.Analyzers/ |
link | https://github.com/marcominerva/MinimalHelpers |
author | Maroc Minerva |
Controller like API registering
This is how you can use MinimalHelpers.Routing.Analyzers .
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="MinimalHelpers.Routing.Analyzers" Version="1.0.13" /> <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; namespace APIDemo; public class PersonAPI : IEndpointRouteHandlerBuilder { public static void MapEndpoints(IEndpointRouteBuilder endpoints) { var grp = endpoints.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; } }
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.MapEndpoints(); 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 /> namespace Microsoft.AspNetCore.Routing; #nullable enable annotations #nullable disable warnings /// <summary> /// Provides extension methods for <see cref="IEndpointRouteBuilder" /> to add route handlers. /// </summary> public static class EndpointRouteBuilderExtensions { /// <summary> /// Automatically registers all the route endpoints defined in classes that implement the <see cref="IEndpointRouteHandlerBuilder "/> interface. /// </summary> /// <param name="endpoints">The <see cref="IEndpointRouteBuilder" /> to add routes to.</param> public static IEndpointRouteBuilder MapEndpoints(this IEndpointRouteBuilder endpoints) { global::APIDemo.PersonAPI.MapEndpoints(endpoints); return endpoints; } }
// <auto-generated /> namespace Microsoft.AspNetCore.Routing; #nullable enable annotations #nullable disable warnings /// <summary> /// Defines a contract for a class that holds one or more route handlers that must be registered by the application. /// </summary> public interface IEndpointRouteHandlerBuilder { /// <summary> /// Maps route endpoints to the corresponding handlers. /// </summary> static abstract void MapEndpoints(IEndpointRouteBuilder endpoints); }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MinimalHelpers.Routing.Analyzers
Leave a Reply