Introduction

In traditional ASP.NET MVC or Web API, we need:

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?

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

When to Use Minimal APIs?

Use Minimal APIs when:

When NOT to Use?

Avoid when:

Real-World Example

Minimal APIs are used in:

Advantages

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: