Minimal APIs in ASP.NET Core

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 Minimal APIs are

  • Why they are trending

  • How to create APIs step-by-step

  • Real example

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:

  • πŸ‘‰ Empty Template or Web API

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

    • MVC

    • Minimal API

  • Code

    • More

    • Less

  • Controllers

    • Required

    • Not required

  • Performance

    • Good

    • Faster

  • Learning

    • Medium

    • Easy

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:

  • Microservices architecture

  • Lightweight backend services

  • Mobile app APIs

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