Introduction: REST API in C# — Fast and Simple
Modern web application and mobile service development is impossible without a REST API. If you want to quickly create a backend for your project, C# with the ASP.NET Core framework is an ideal choice. Thanks to built-in tools, minimal configuration, and excellent performance, you can write a full-fledged REST API in literally 30 minutes.
In this article, we will walk through the process of creating a simple API for managing a Todo List. You will learn how to set up the project, create data models, implement controllers with CRUD operations, and test the API using Swagger. All code examples are written in C# (.NET 8).
Even if you are a beginner in C#, by following the instructions, you will have a working API by the end of the article.
1. Creating the Project and Setting Up the Environment
First, make sure you have the .NET SDK (version 8 or higher) installed. You can download it from the official website. After installation, open a terminal or command prompt.
Create a new ASP.NET Core Web API project with the command:
dotnet new webapi -n TodoApicd TodoApiThis command will create a TodoApi folder with the basic project structure. It already contains a Program.cs file — the entry point, and a Controllers folder for future controllers.
Open the project in Visual Studio, Visual Studio Code, or Rider. For simplicity, you can use any text editor.
Make sure the Program.cs file contains calls to AddSwaggerGen() and UseSwagger(). Swagger is automatically included in the template — this will simplify testing. If it is not there, add:
builder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();// ...app.UseSwagger();app.UseSwaggerUI();2. Creating the Data Model and Temporary Storage
For our API, we will store tasks in memory (in-memory). In a real project, you would connect a database, but for demonstrating a 30-minute solution, this is sufficient.
Create a Models folder and inside it, a file TodoItem.cs:
public class TodoItem{ public int Id { get; set; } public string Title { get; set; } = string.Empty; public bool IsCompleted { get; set; }}Now let's create a simple service-repository. In the Services folder, create an interface ITodoService.cs and its implementation TodoService.cs. This will make it easy to switch to a real database in the future.
ITodoService.cs:
public interface ITodoService{ List<TodoItem> GetAll(); TodoItem? GetById(int id); TodoItem Create(TodoItem item); TodoItem? Update(int id, TodoItem item); bool Delete(int id);}TodoService.cs:
public class TodoService : ITodoService{ private readonly List<TodoItem> _items = new(); private int _nextId = 1;
public List<TodoItem> GetAll() => _items;
public TodoItem? GetById(int id) => _items.FirstOrDefault(i => i.Id == id);
public TodoItem Create(TodoItem item) { item.Id = _nextId++; _items.Add(item); return item; }
public TodoItem? Update(int id, TodoItem item) { var existing = GetById(id); if (existing == null) return null; existing.Title = item.Title; existing.IsCompleted = item.IsCompleted; return existing; }
public bool Delete(int id) { var item = GetById(id); if (item == null) return false; return _items.Remove(item); }}Register the service in the DI container in Program.cs:
Blogs
Book Recommendations
Read also
C# in the World of Microcontrollers: An Unexpected Breakthrough in Embedded
Global Singleton in Rust: Breaking Stereotypes with Code
Introduction to Go in 2025: Where to Start for Beginners
Feature flags in C++: managing features without pain and rebuilding
Introduction to Go: Where to Start in 2025 — A Beginner's Guide