Introduction
In traditional ASP.NET MVC or Web API, we need:
Controllers
Models
Routing setup
But in modern ASP.NET Core, Microsoft introduced Minimal APIs.
Minimal APIs allow us to create APIs with less code, faster performance, and simple structure.
In this article, you will learn:
What are Minimal APIs?
Minimal APIs are a way to create APIs using very little code without controllers.
Example:
app.MapGet("/hello", () => "Hello World");
Thatβs it! π
No controller, no extra setup.
Why Minimal APIs are Trending?
β Less code
β Faster development
β High performance
β Easy to learn
Step 1: Create Project
Create a new ASP.NET Core Web Application.
Choose:
Step 2: Write First API
Open Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Welcome to Minimal API");
app.Run();
Output
Welcome to Minimal API
Step 3: Create GET API
app.MapGet("/students", () =>
{
return new List<string> { "Abhay", "Rahul", "Amit" };
});
Output
["Abhay","Rahul","Amit"]
Step 4: Create POST API
app.MapPost("/add", (string name) =>
{
return $"Student {name} added successfully";
});
Step 5: Use Model
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
app.MapPost("/student", (Student s) =>
{
return s;
});
Step 6: Route Parameters
app.MapGet("/student/{id}", (int id) =>
{
return $"Student Id: {id}";
});
Step 7: Dependency Injection
app.MapGet("/service", (IMyService service) =>
{
return service.GetData();
});
Minimal API vs MVC
Feature
Code
Controllers
Performance
Learning
When to Use Minimal APIs?
Use Minimal APIs when:
β Building small services
β Creating microservices
β Fast API development needed
β Lightweight applications
When NOT to Use?
Avoid when:
β Large enterprise apps
β Complex business logic
β Need full MVC structure
Real-World Example
Minimal APIs are used in:
Advantages
β Simple syntax
β Fast execution
β Clean code
β Beginner-friendly
Conclusion
Minimal APIs are a modern way to build APIs in ASP.NET Core with less code and better performance.
In this article, we learned:
What Minimal APIs are
How to create APIs
When to use them