Module 2: Minimal APIs (The Fast Food Stand)
📚 Module 2: Minimal APIs
Course ID: DOTNET-302
Subject: The Fast Food Stand
MVC (Module 1) is like a Fine Dining Restaurant. It has waiters, hosts, and many rooms. Minimal APIs are like a Fast Food Stand. It’s just one person, a grill, and a window. It’s significantly faster and has zero boilerplate.
🏗️ Step 1: The Boilerplate Problem
In MVC, to return a “Hello World” string, you need:
- A
Controllerclass. - A
using Microsoft.AspNetCore.Mvc. - A
public IActionResult. - A correctly named folder for your View.
Minimal APIs do it in one line.
🏗️ Step 2: The Map Command (The “Direct Order”)
In a Minimal API, you map the URL directly to a function.
🧩 The Analogy: The Drive-Thru
- You drive up to the window (The URL
/hello). - You say what you want.
- They hand it to you instantly. No waiters, no menus, no table settings.
In Code:
var app = builder.Build();
// That's it! This is a complete API.
app.MapGet("/hello", () => "Hello from the Fast Food Stand!");
app.Run();🏗️ Step 3: When to use Minimal APIs?
- Microservices: When your service only has 2 or 3 endpoints.
- Performance: Minimal APIs are about 10-20% faster than Controllers because they don’t have to “Look in the Mirror” (Reflection) as much.
- Modern Frontends: Perfect for React, Vue, or Blazor backends.
🧪 Step 4: Python Practice (C# vs. FastAPI)
If you have used FastAPI in Python, Minimal APIs will feel like home.
# FastAPI
@app.get("/items/{id}")
def read_item(id: int):
return {"id": id}// .NET Minimal API
app.MapGet("/items/{id}", (int id) => new { id });🥅 Module 2 Review
- Minimal API: High-performance endpoints with zero classes or folders required.
- MapGet/MapPost: Routing your logic directly to a URL.
- Performance: The leanest way to build a web service in .NET.