Minimal APIs
- You can create HTTP APIs with minimal dependencies
- Ideal for microservices and apps that want to include only minimal files, features and dependencies in ASP.NET Core
Minimal API Code
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World");
app.Run();
WebApplication
class from Microsoft.AspNetCore
is used to configure the pipeline and routes. Use the CreateBuilder
static method to create a new instance of WebApplicationBuilder
Build the application.
Creates an HTTP GET endpoint /
which returns Hello World!
Run the app
Todo CRUD API
In this sample you will create a Todo CRUD API with Entity Framework Core in-memory database provider
Create ASP.NET Core Web API
- Open Visual Studio 2022
- Create new project > ASP.NET Core Web API > Next
- Enter project name
TodoApi
and click next - In framework select .NET 6.0
- Uncheck use controllers to use minimal API
- Click Create
Install Nuget Packages
Microsoft.EntityFrameworkCore.InMemory
In-memory database provider for Entity Framework Core. Used for testing purposes.
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
ASP.NET Core middleware for Entity Framework Core error pages. Use this middleware to detect and diagnose errors with Entity Framework Core migrations.
Create Model Todo
namespace TodoApi;
public class Todo
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
}
Create TodoDb DbContext
namespace TodoApi;
public class TodoDb: DbContext
{
public TodoDb(DbContextOptions<TodoDb> options): base(options)
{
}
public DbSet<Todo> Todos => Set<Todo>();
}
Add API Code
using Microsoft.EntityFrameworkCore;
using TodoApi;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.MapGet("/todoitems", async (TodoDb db) => await db.Todos.ToListAsync());
app.MapGet("/todoitems/complete", async(TodoDb db) => await db.Todos.Where(t => t.IsComplete).ToListAsync());
app.MapGet("/todoitems/{id}", async (int id, TodoDb db) => await db.Todos.FindAsync(id) is Todo todo ? Results.Ok(todo) : Results.NotFound());
app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
{
db.Todos.Add(todo);
await db.SaveChangesAsync();
return Results.Created($"/todoitems/{todo.Id}", todo);
});
app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
{
var todo = await db.Todos.FindAsync(id);
if (todo is null) return Results.NotFound();
todo.Name = inputTodo.Name;
todo.IsComplete = inputTodo.IsComplete;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
{
if (await db.Todos.FindAsync(id) is Todo todo)
{
db.Todos.Remove(todo);
await db.SaveChangesAsync();
return Results.Ok(todo);
}
return Results.NotFound();
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.Run();
In the previous versions, the host builder in configured in Program.cs
and you use ConfigureServices
to add the services to the container and Configure
to configure the HTTP request pipeline in the Startup.cs
From ASP.NET Core 6.0, all the configurations are done in Program.cs
itself. You now use builder.Services.AddXXX
method syntax to add services to the container and app.UseXXX()
method syntax to configure the pipeline
Run the Application
Run the application. Use the Swagger UI to test the API.