AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

sample code: Provide endpoint descriptions and summaries for minimal APIs

Open Rick-Anderson opened this issue 3 years ago • 4 comments

See Provide endpoint descriptions and summaries for minimal APIs

and write sample code in https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/endpoint-description-summary/7.0-samples

then delete https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/endpoint-description-summary/7.0-samples/ReadMe.md

cc @fiyazbinhasan

Rick-Anderson avatar Jul 01 '22 23:07 Rick-Anderson

on it! :)

fiyazbinhasan avatar Jul 03 '22 08:07 fiyazbinhasan

@Rick-Anderson There seems to be an issue with OpenAPI support. I can't see any summary or description,

image

fiyazbinhasan avatar Jul 07 '22 05:07 fiyazbinhasan

ok I can see there is an opened issue for this https://github.com/dotnet/aspnetcore/issues/40753

fiyazbinhasan avatar Jul 07 '22 05:07 fiyazbinhasan

We'll have to wait.

using Microsoft.AspNetCore.OpenApi;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

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("/", [EndpointSummary("Sends a Hello request to the backend")]   // <----
[EndpointDescription("Description: Sends hello request")] () => "Hello World!");   // <----

app.MapGet("/weatherforecast", () =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateTime.Now.AddDays(index),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithDescription("Description:Sends the weather forcast.")         // <----
.WithSummary("Summary: Sends the weather forcast.")   // <----
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

Rick-Anderson avatar Jul 07 '22 18:07 Rick-Anderson