Minimal API CRUD in .NET 10 step by step

Minimal API CRUD in .NET 10 Step by Step

In this article, we will build a complete CRUD (Create, Read, Update, Delete) application using Minimal APIs in .NET 10. Minimal APIs allow you to create lightweight and fast web APIs with less code and better performance.

  • Create ASP.NET Core Minimal API Project
  • Create Model Class
  • Setup In-Memory Database
  • Create CRUD Endpoints
  • Test API using Browser / Swagger

Step 1 : Create Project

Create a new Minimal API project using .NET CLI:

dotnet new web -n MinimalApiDemo
cd MinimalApiDemo

Step 2 : Create Model Class (Product.cs)
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

This model represents the data we will store and manipulate.


Step 3 : Setup In-Memory Data

Instead of using a database, we will use a simple in-memory list.

List<Product> products = new List<Product>()
{
    new Product { Id = 1, Name = "Laptop", Price = 1000 },
    new Product { Id = 2, Name = "Mobile", Price = 500 }
};

Step 4 : Create CRUD APIs (Program.cs)

Open Program.cs and write the following code:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

List<Product> products = new List<Product>()
{
    new Product { Id = 1, Name = "Laptop", Price = 1000 },
    new Product { Id = 2, Name = "Mobile", Price = 500 }
};

// GET ALL
app.MapGet("/products", () => products);

// GET BY ID
app.MapGet("/products/{id}", (int id) =>
{
    var product = products.FirstOrDefault(x => x.Id == id);
    return product is not null ? Results.Ok(product) : Results.NotFound();
});

// CREATE
app.MapPost("/products", (Product product) =>
{
    product.Id = products.Max(x => x.Id) + 1;
    products.Add(product);
    return Results.Created($"/products/{product.Id}", product);
});

// UPDATE
app.MapPut("/products/{id}", (int id, Product updatedProduct) =>
{
    var product = products.FirstOrDefault(x => x.Id == id);

    if (product is null)
        return Results.NotFound();

    product.Name = updatedProduct.Name;
    product.Price = updatedProduct.Price;

    return Results.Ok(product);
});

// DELETE
app.MapDelete("/products/{id}", (int id) =>
{
    var product = products.FirstOrDefault(x => x.Id == id);

    if (product is null)
        return Results.NotFound();

    products.Remove(product);
    return Results.Ok();
});

app.Run();

Step 5 : Run the Application
dotnet run

Open browser:

  • GET All → http://localhost:5000/products
  • GET By Id → http://localhost:5000/products/1

Testing using JSON
POST /products
{
  "name": "Tablet",
  "price": 300
}

PUT /products/1
{
  "name": "Updated Laptop",
  "price": 1200
}

DELETE /products/1


Note : This example uses in-memory data. For real applications, use Entity Framework Core with SQL Server.

Conclusion

In this article, we learned how to build a complete CRUD API using Minimal APIs in .NET 10. Minimal APIs reduce boilerplate code and make development faster. You can extend this example by adding database support, authentication, and validation.

Post a Comment