Minimal APIs & Controllers
Learn both traditional controllers and modern minimal APIs for building web endpoints. This is a foundational concept in cross-platform .NET development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world .NET Core experience. Take your time with each section and practice the examples
70 min•By Priygop Team•Last updated: Feb 2026
Minimal APIs
Example
// Simple GET endpoint
app.MapGet("/hello", () => "Hello World!");
// GET with parameters
app.MapGet("/hello/{name}", (string name) => $"Hello {name}!");
// POST with JSON body
app.MapPost("/users", (User user) => Results.Created($"/users/{user.Id}", user));
// PUT with validation
app.MapPut("/users/{id}", (int id, User user) =>
{
// Update user logic
return Results.Ok(user);
});
// DELETE endpoint
app.MapDelete("/users/{id}", (int id) =>
{
// Delete user logic
return Results.NoContent();
});Traditional Controllers
Example
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
var users = await _userService.GetAllUsersAsync();
return Ok(users);
}
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(int id)
{
var user = await _userService.GetUserByIdAsync(id);
if (user == null)
return NotFound();
return Ok(user);
}
[HttpPost]
public async Task<ActionResult<User>> CreateUser(CreateUserRequest request)
{
var user = await _userService.CreateUserAsync(request);
return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
}
}