Introduction
Minimal API usage .NET and C# offer a fast, lightweight, and modern approach to building HTTP APIs using minimal configuration and boilerplate code. Introduced in .NET 6Minimal APIs are becoming increasingly popular microservices, cloud-native applicationAnd rapid prototyping because of its simplicity and performance. In this blog, you will learn how to create Minimal API step by step using .NET and C#set up an endpoint, and run your first lightweight API project.
Prerequisites for Building a Minimal API in .NET 6
Before starting, make sure you have:
- .NET SDK installed (version 6 or later)
- Code editor (Such as Visual Studio, VS Code, or JetBrains Rider)
- Basic knowledge about C#, REST APIAnd HTTP method
Create a Minimal API Using .NET 6 and C# Project Setup
Open your terminal or command prompt and create a new Minimal API project using the following command:
dotnet new web -n MinimalApiDemo
Basically, this command creates a new web project called MinimalApiDemo.
- Navigate to the Project Directory
cd MinimalApiDemo
- Open the Project in your Editor
In the next step, open the project in your favorite code editor to explore and work with the files.
Defining Models in a Minimal API
Let’s start by creating a simple model for a Todo item. In your project folder, create a new file called TodoItem.cs and add the code below:
public class TodoItem
{
public int Id { get; set; }
public string? Title { get; set; }
public bool IsCompleted { get; set; }
}
Setting Up an API Endpoint Using .NET 6 and C#
Now, let’s define an API endpoint that will handle various HTTP requests. To do this, open it Program.cs file and replace or update existing code with the example below:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
List<TodoItem> todos = new();
app.MapGet("/todos", () => todos);
app.MapGet("/todos/{id}", (int id) =>
todos.FirstOrDefault(t => t.Id == id)
);
app.MapPost("/todos", (TodoItem todo) =>
{
todo.Id = todos.Count + 1;
todos.Add(todo);
return Results.Created($"/todos/{todo.Id}", todo);
});
app.MapPut("/todos/{id}", (int id, TodoItem updatedTodo) =>
{
var todo = todos.FirstOrDefault(t => t.Id == id);
if (todo is null) return Results.NotFound();
todo.Title = updatedTodo.Title;
todo.IsCompleted = updatedTodo.IsCompleted;
return Results.Ok(todo);
});
app.MapDelete("/todos/{id}", (int id) =>
{
var todo = todos.FirstOrDefault(t => t.Id == id);
if (todo is null) return Results.NotFound();
todos.Remove(todo);
return Results.NoContent();
});
app.Run();
Code Explanation
Dependency Injection
We use a single list to store TodoItem object, providing a simple in-memory storage mechanism for demonstration purposes.
API Endpoint
- GET /all → Retrieve all plan items
- GET /todos/{id} → Get plan items by ID
- POST /all → Create a new plan item
- PLACE /todos/{id} → Update plan items
- DELETE /todos/{id} → Delete each item
Running APIs
Basically, to run the API, run the following command in your terminal:
dotnet run
Once the application is running, your API will be accessible at http://localhost:5000. You can test endpoints using tools like Postman or curly.
Running and Testing Minimal API
Sample Request
- Create a new Todo item using a POST request
curl -X POST -H "Content-type: application/json" -d '{"title": "Learn Minimal APIs", "isCompleted": false}'
- Next, we will retrieve all Todo items using a GET request
curl -X GET /1 -H "Content-type: application/json" -d '{"title": "Learn Minimal APIs", "isCompleted": true}'
- Modify Existing Todo Items
curl -X PUT /1 -H "Content-type: application/json" -d '{"title": "Learn Minimal APIs", "isCompleted": true}'
curl -X DELETE /1
Conclusion
A minimal API using .NET provides a clean, fast, and efficient way to build small-scale services without the overhead of traditional controllers. They are perfect for microservices, cloud-native applications, prototypingand scenarios that prioritize performance and simplicity.
With just a few lines of C# code, you can create a fully functional endpoint, test it quickly, and scale from a simple demo to a production-ready service.
Whether you're a beginner or an experienced .NET developer, Minimal API is a powerful addition to your development toolkit.
News
Berita
News Flash
Blog
Technology
Sports
Sport
Football
Tips
Finance
Berita Terkini
Berita Terbaru
Berita Kekinian
News
Berita Terkini
Olahraga
Pasang Internet Myrepublic
Jasa Import China
Jasa Import Door to Door