Introduction
Minimal APIs were introduced in .NET 6 to simplify the creation of small, fast, and lightweight web APIs. They allow developers to write API logic with less code and configuration, without needing controllers or attributes like in traditional ASP.NET MVC.
They are best used when building simple services, microservices, or proof-of-concept applications.
1. Create a New Minimal API Project
To get started
dotnet new web -n MinimalApiDemo
- This template sets up a basic minimal API.
- No controllers or Views folder.
2. Basic Program.cs Structure
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
MapGetdefines a GET endpoint.- Everything is written in
Program.cs.
3. MapGet() – Handle GET Requests
app.MapGet("/hello", () => "Hi there!");
- Used to return data or simple messages.
- Can return strings, objects, or JSON.
4. MapPost() – Handle POST Requests
app.MapPost("/add", (int x, int y) => x + y);
- Accepts parameters directly from query or body.
- Ideal for submitting data.
5. MapPut() – Handle PUT Requests
app.MapPut("/update", (int id, string name) => $"Updated {id} to {name}");
-
Used to update existing resources.
6. MapDelete() – Handle DELETE Requests
app.MapDelete("/delete/{id}", (int id) => $"Deleted item {id}");
-
Deletes a resource based on the ID.
7. Return JSON Response
app.MapGet("/user", () => new { Name = "John", Age = 25 });
- Automatically serializes object to JSON.
- No need for extra configuration.
8. Reading From Query Strings
app.MapGet("/greet", (string name) => $"Hello, {name}!");
-
Pass like
/greet?name=Diksha.
9. Reading From Route Parameters
app.MapGet("/product/{id}", (int id) => $"Product ID: {id}");
-
Route parameters are defined inside
{}.
10. Read JSON Body in POST
app.MapPost("/person", (Person person) =>
{
return $"Name: {person.Name}, Age: {person.Age}";
});
record Person(string Name, int Age);
- Automatically binds the JSON request body to an object.
- Use
recordorclass.
11. Dependency Injection (DI)
Register service
builder.Services.AddSingleton<MyService>();
Use it in the route
app.MapGet("/info", (MyService svc) => svc.GetInfo());
-
Supports constructor-less DI in lambda.
12. Validation Example
app.MapPost("/validate", (User user) =>
{
if (string.IsNullOrWhiteSpace(user.Email))
return Results.BadRequest("Email is required");
return Results.Ok("Valid User");
});
record User(string Email);
-
Manual validation can be added easily.
13. Using Results Object
app.MapGet("/status", () => Results.Ok("Working"));
app.MapGet("/fail", () => Results.BadRequest("Something went wrong"));
-
Explicit control over HTTP response type and status.
14. Grouping Routes
var userGroup = app.MapGroup("/users");
userGroup.MapGet("/", () => "All users");
userGroup.MapPost("/", (User u) => $"User {u.Email} added");
-
Helps organize related endpoints under a common path.
15. Middleware Usage
app.Use(async (context, next) =>
{
Console.WriteLine("Request coming in");
await next();
Console.WriteLine("Response going out");
});
-
Minimal APIs support middlewares like normal APIs.
16. Enable Swagger (OpenAPI)
Add services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Use Swagger UI
app.UseSwagger();
app.UseSwaggerUI();
-
Helps with testing and documentation.
17. Route Constraints
app.MapGet("/item/{id:int}", (int id) => $"Item ID: {id}");
-
:intensures only integer is accepted.

Join the conversation! Your thoughts help the community grow.