dotnet/aspnetcore

Getting started with minimal apis end to end

LadyNaggaga opened this issue ยท 0 comments

This issue intends to capture a smooth getting started experience with minimal APIs. Our goal is to have all experience smoothed (๐Ÿ˜บ ) out before we will move the new minimal api template from a package to OOB with the SDK.

Current user Experience Description
๐Ÿ˜บ Smooth experience
๐Ÿ˜พ Bumpy experience
๐Ÿ™€ Confusing / Overwhelming experience
๐ŸŽฉ Stretch goal for the product
โœ”๏ธ Available in the empty template OOB. No additional configuration required from the developer

Acquisition

Note : To try out minimal APIs for hosting and routing in web applications we announced in .NET 6 preview 4 today, create a new ASP.NET Core empty web app dotnet new web -o MinApi. โœ”๏ธ

  • Phase 1: Minimal APIs template as a package

  • Install package Microsoft.AspNetCore.Minimal-api.Templates
    dotnet new install Microsoft.AspNetCore.MinimalApi.Templates:: 0.1.0-pre

  • Create a new minimal api
    dotnet new api -o myapi

  • Phase 2: New minimal APIs template shipped in .NET SDK

  • User installs .NET SDK

  • API template surfaces in the .NET CLI and Visual Studio new project dialogue.

Define a new endpoint โœ”๏ธ ๐Ÿ˜บ

  • User can easily define a new endpoint

app.MapGet("/foo", () => "I am new endpoint");

Open API support

  • [ x] Option 1: Add Swagger UI to your application

Install the Microsoft Open API and Swagger packages.

Using .NET CLI

Microsoft open API

dotnet add package Swashbuckle.AspNetCore --version 6.1.4

In Visual Studio

In Visual Studio you can use the Package Manager Console or Manage Nuget Package GUI.

Install-Package Swashbuckle.AspNetCore -Version 6.1.4

Configure your the Swagger UI yourself

User configures Swagger UI on their own with the following snippets

Snippet 1: Below var builder = WebApplication.CreateBuilder(args);

add the following lines of code

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo API", Description = "Keep track of your tasks", Version = "v1" });
});

Snippet 2: Above app.MapGet("/", () => "Hello World!");
add the following lines of code

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Todo API V1");
});
  • Option 2: Swagger Built into the template ๐Ÿ˜บ

Alternative path: Swagger turned on my default

Example: user runs their app and navigates to this URL https://localhost:5001/docs/. No addtional code required.

docs-swagger

Working with data ๐Ÿ˜พ

Working with In-memory database Microsoft.EntityFrameworkCore.InMemory
Implement - GET, POST, PUT, DELETE.

app.MapGet("/todo", () => new[] { new TodoItem { Id = 1, Item = "Water Plants", IsComplete = true } });

app.MapGet("/todos", async (TodoDb db) => await db.Todos.ToListAsync());

app.MapGet("/todos/{id}", async (TodoDb db, int id) => await db.Todos.FindAsync(id));

app.MapPost("/todos", async (TodoDb db,TodoItem todo) => 
{
    await db.Todos.AddAsync(todo);
    await db.SaveChangesAsync();
    return Created($"/todo/{todo.Id}", todo);
});


app.MapDelete("/todos/{id}", async ( TodoDb db, int id) =>
{
    var todo = await db.Todos.FindAsync(id);
    if (todo is null)
    {
        return NotFound();
    }
    db.Todos.Remove(todo);
    await db.SaveChangesAsync();

    return Ok();

});

image

Validation

details coming soon

Authorization

details coming soon

Deploy

details coming soon

Error messages