projects/mini/minimal-api-pokedex/Readme.md
This projet showcases the new .NET 6 Minimal APIs feature for developing Web APIs. We build a Pokedex API in this sample.
The project uses a static pokedex JSON file as a data store to read. The JSON file is kept in Data folder and read at runtime. We setup Swagger as part of the project and we get a Swagger UI to look at the endpoints supported. Steps to set up a minimal api:
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options => options.SwaggerDoc("v1", new OpenApiInfo()
{
Description = "Pokedex API implemetation using .NET 6 Minimal API",
Title = "Pokedex API",
Version = "v1",
Contact = new OpenApiContact()
{
Name = "Lohith GN",
Url = new Uri("https://github.com/lohithgn")
}
}));
services.AddScoped<IPokedexRepository, PokedexRepository>();
services.AddScoped<IPokedexService, PokedexService>();
var app = builder.Build();
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Pokedex Api v1");
c.RoutePrefix = string.Empty;
});
builder.MapGet("/pokedex", async (int? page, int? pageSize, IPokedexService service) =>
{
//...call to service
})
.Produces<PokedexPagedResponse>(StatusCodes.Status200OK);
builder.MapGet("/pokedex/all", async (IPokedexService service) =>
{
//...call to service
})
.Produces<PokedexResponse>(StatusCodes.Status200OK);
builder.MapGet("/pokedex/{name}", async (string name, IPokedexService service) =>
{
//...call to service
})
.Produces<PokemonEntity>(StatusCodes.Status200OK)
.ProducesProblem(StatusCodes.Status404NotFound);
builder.MapGet("/pokedex/search", async (string query, int? page, int? pageSize, IPokedexService service) =>
{
//...call to service
})
.Produces<PokedexPagedResponse>(StatusCodes.Status200OK)
.Produces<PokedexPagedResponse>(StatusCodes.Status400BadRequest);
app.Run();
Note: To run this sample, you need to have .NET 6 installed on your machine. If you are using Visual Studio Code make sure to install the latest .NET 6 SDK. If you are using Visual Studio - make sure you use VS2022 as it comes with .NET 6.
Contribution by Lohith G. N.