CRUD names the four operations used to manage persistent data: create, read, update, and delete. In a minimal API, different HTTP verbs can expose these operations over a collection or database. section 5.2.3 section 12.0
A typical CRUD API assigns one operation to each endpoint action:
- Create adds a new record, commonly through POST. section 5.2.3
- Read retrieves a collection or an individual record, commonly through GET. section 5.2.3
- Update changes an existing record, commonly through PUT. section 5.2.3 section 12.4.4
- Delete removes or marks a record as deleted, depending on the application’s data policy. section 5.2.3 section 12.4.4
Updating an entity with EF Core
A reliable update flow reads the entity by ID, changes its properties, and saves the changes. Keeping this work in a service rather than the API handler separates application transport from business logic. section 12.4.4
Listing 5.2 Creating route handlers for a simple CRUD API
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
app.MapGet("/fruit", () => Fruit.All); #1
var getFruit = (string id) => Fruit.All[id]; #2
app.MapGet("/fruit/{id}", getFruit); #2
app.MapPost("/fruit/{id}", Handlers.AddFruit); #3
Handlers handlers = new(); #4
app.MapPut("/fruit/{id}", handlers.ReplaceFruit); #4
app.MapDelete("/fruit/{id}", DeleteFruit); #5
app.Run();
void DeleteFruit(string id) #5
{
Fruit.All.Remove(id);
}
record Fruit(string Name, int Stock)
{
public static readonly Dictionary<string, Fruit> All = new();
};
class Handlers
{
public void ReplaceFruit(string id, Fruit fruit) #6
{
Fruit.All[id] = fruit;
}
public static void AddFruit(string id, Fruit fruit) #7
{
Fruit.All.Add(id, fruit);
}
}
Inspect a compact minimal API that maps GET, POST, PUT, and DELETE handlers to a Fruit collection. section 5.2.3










