Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

.NET Core: Build Lightweight APIs with Minimal APIs

- 09.07.26 - ErcanOPAK

⚡ Minimal APIs = Lightweight Endpoints

Controllers are heavy. Minimal APIs are lightweight. Single-file APIs, faster development, microservices.

📝 Minimal API Setup

// Program.cs (Minimal API)
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Basic endpoints
app.MapGet("/", () => "Hello World!");

app.MapGet("/users", () => new[] { 
    new { Id = 1, Name = "Alice" },
    new { Id = 2, Name = "Bob" }
});

app.MapGet("/users/{id:int}", (int id) => 
    new { Id = id, Name = $"User {id}" }
);

app.MapPost("/users", (User user) => {
    // Create user
    return Results.Created($"/users/{user.Id}", user);
});

app.MapPut("/users/{id:int}", (int id, User user) => {
    // Update user
    return Results.Ok(user);
});

app.MapDelete("/users/{id:int}", (int id) => {
    // Delete user
    return Results.NoContent();
});

app.Run();

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

🎯 Advanced Minimal APIs

// Dependency Injection
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton();
builder.Services.AddDbContext();

var app = builder.Build();

app.MapGet("/users", async (IUserService service) => 
    await service.GetAllUsersAsync()
);

// Validation
app.MapPost("/users", async (User user, IUserService service) => {
    if (string.IsNullOrEmpty(user.Name))
        return Results.BadRequest("Name required");
    
    var created = await service.CreateUserAsync(user);
    return Results.Created($"/users/{created.Id}", created);
});

// OpenAPI (Swagger)
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
app.UseSwagger();
app.UseSwaggerUI();

// Group endpoints
var usersGroup = app.MapGroup("/api/users");
usersGroup.MapGet("/", GetUsers);
usersGroup.MapGet("/{id:int}", GetUser);
usersGroup.MapPost("/", CreateUser);

// With tags
app.MapGet("/health", () => "Healthy")
   .WithTags("Health")
   .WithName("HealthCheck")
   .Produces(200);

// With filters
app.MapGet("/secure", () => "Secure")
   .AddEndpointFilter(async (context, next) => {
       var auth = context.HttpContext.Request.Headers["Authorization"];
       if (string.IsNullOrEmpty(auth))
           return Results.Unauthorized();
       
       return await next(context);
   });

// Return multiple types
app.MapGet("/data/{id:int}", (int id) => {
    if (id <= 0)
        return Results.BadRequest("Invalid ID");
    
    if (id > 100)
        return Results.NotFound();
    
    return Results.Ok(new { Id = id, Data = $"Data {id}" });
});

💡 Minimal API Benefits

  • Less boilerplate
  • Faster development
  • Single-file microservices
  • Lower learning curve
  • Great for simple APIs

“Minimal APIs simplify microservices. Lightweight, fast, easy. Essential for modern .NET development.”

— .NET Developer

Related posts:

.NET Core: Global Exception Handling with DI

.NET Core: Global Exception Handling with DI

Asp.Net Core: Fix Rate Limiting Resetting on Every Deploy

Asp.Net Core: Fix Rate Limiting Resetting on Every Deploy

Background Tasks with IHostedService

Background Tasks with IHostedService

Views: 17
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Git: Use Git LFS for Large File Storage
SQL: Master Query Optimization for Performance

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com