Introduction
ASP.NET Core has evolved a lot over time, and one of the most powerful features introduced is Minimal APIs. With the latest ASP.NET Core 9, developers can build fast, lightweight, and scalable web applications with very little code.
If you are a beginner or even an experienced .NET developer, learning how to build a Minimal ASP.NET Core application can help you create APIs quickly without unnecessary complexity.
In this article, we will explain how to build a Minimal ASP.NET Core 9 application step-by-step. We will also explore its benefits, real-world use cases, and best practices.
What is a Minimal API in ASP.NET Core?
A Minimal API is a simplified way to build HTTP APIs in ASP.NET Core without using controllers.
In traditional ASP.NET Core:
In Minimal API:
Simple meaning:
"Write less code, build APIs faster."
Why Minimal APIs Are Important
Minimal APIs are designed for:
Simplicity
Performance
Faster development
They are especially useful when:
This makes them a popular choice in modern .NET development.
Step-by-Step: Build a Minimal ASP.NET Core 9 Application
Let’s build a simple API.
Step 1: Create a New Project
Use .NET CLI:
dotnet new web -n MinimalApiDemo
cd MinimalApiDemo
This creates a minimal ASP.NET Core project.
Step 2: Understand Program.cs
Open Program.cs. You will see very little code.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World from ASP.NET Core 9 Minimal API!");
app.Run();
Explanation:
WebApplication.CreateBuilder → sets up app
builder.Build() → builds app
MapGet → defines endpoint
Run() → starts server
Step 3: Run the Application
dotnet run
Open browser:
https://localhost:5001
Output:
Hello World from ASP.NET Core 9 Minimal API!
Step 4: Add More Endpoints
app.MapGet("/users", () => new[]
{
new { Id = 1, Name = "John" },
new { Id = 2, Name = "Alice" }
});
Now your API returns JSON data.
Step 5: Use Route Parameters
app.MapGet("/users/{id}", (int id) =>
{
return $"User ID: {id}";
});
This allows dynamic data handling.
Step 6: Use Dependency Injection
builder.Services.AddSingleton<MyService>();
app.MapGet("/service", (MyService service) =>
{
return service.GetMessage();
});
public class MyService
{
public string GetMessage() => "Hello from Service";
}
Dependency Injection works the same as in full ASP.NET Core.
Step 7: Handle POST Requests
app.MapPost("/users", (User user) =>
{
return Results.Ok(user);
});
public record User(int Id, string Name);
This allows you to create data via API.
Benefits of Minimal ASP.NET Core 9
Less Boilerplate Code
Minimal APIs remove unnecessary files like controllers.
Result:
Cleaner code
Easier to read
Faster to write
High Performance
Minimal APIs are faster because:
Less abstraction
Direct request handling
This makes them ideal for high-performance applications.
Faster Development
You can build APIs quickly with fewer lines of code.
Perfect for:
Startups
MVP development
Hackathons
Easy to Learn
Beginners can easily understand Minimal APIs.
No need to learn complex MVC structure.
Better for Microservices
Minimal APIs are lightweight and perfect for microservices architecture.
Each service can be small and independent.
Flexible and Extensible
You can still use:
Middleware
Filters
Dependency Injection
So you don’t lose power.
Real-World Example
Imagine you are building a product API:
app.MapGet("/products", () => new[]
{
new { Id = 1, Name = "Laptop", Price = 50000 },
new { Id = 2, Name = "Phone", Price = 20000 }
});
This is enough to create a working API quickly.
Minimal API vs Traditional Controller API
| Feature | Minimal API | Controller API |
|---|
| Code Size | Very small | Large |
| Complexity | Low | Medium/High |
| Performance | High | Slightly lower |
| Learning Curve | Easy | Moderate |
| Best For | Small APIs | Large applications |
When to Use Minimal APIs
Use Minimal APIs when:
Building small services
Creating quick APIs
Performance is critical
Avoid when:
Large enterprise apps
Complex business logic
Need full MVC features
Best Practices
Keep endpoints small and focused
Use Dependency Injection properly
Validate input data
Organize code as project grows
Summary
Minimal APIs in ASP.NET Core 9 provide a simple, fast, and efficient way to build web APIs. With less code and high performance, they are perfect for modern applications, microservices, and quick development. By understanding how to create endpoints, use dependency injection, and handle requests, developers can build scalable and production-ready APIs easily.