Introduction
In my day-to-day development work, almost every application I build or maintain depends on APIs. Whether it is a web dashboard, a mobile app, or even a background service, RESTful APIs act as the backbone that connects everything together.
When I first started working with ASP.NET Core Web APIs, I struggled to understand where to start, how controllers talk to data, and how real projects are structured beyond tutorials. Most examples looked perfect on paper but didn’t explain why certain decisions were made.
In this article, I’ll walk you through building a simple but practical RESTful API using ASP.NET Core, based on a Book Management System. This is not just theory — it’s the same pattern I personally use as a foundation before integrating databases, authentication, or cloud deployment.
Step 1: Setting Up the ASP.NET Core Web API Project
I prefer starting with the CLI because it gives a clean, minimal setup and avoids unnecessary UI confusion.
dotnet new webapi -n BookStoreAPI
cd BookStoreAPI
This command creates:
A ready-to-run Web API project
Basic controller setup
Swagger enabled by default (very useful for testing)
At this stage, you can already run the project and see Swagger working, which helps beginners gain confidence early.
Step 2: Defining the Model (Book)
Before writing APIs, I always define what data I’m working with. For this example, we’ll manage books.
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public decimal Price { get; set; }
}
Why this matters:
Models represent real-world entities
They become the contract between client and server
Later, the same model can map directly to a database table
Step 3: Creating a Repository Layer (Why I Use It)
Many beginners directly write logic inside controllers. I used to do that too — until projects grew and became hard to maintain.
To keep things clean, I follow a repository pattern, even for small projects.
Interface
public interface IBookRepository
{
IEnumerable<Book> GetAll();
Book GetById(int id);
void Add(Book book);
void Update(Book book);
void Delete(int id);
}
Implementation (In-Memory Data)
public class BookRepository : IBookRepository
{
private readonly List<Book> _books = new();
public IEnumerable<Book> GetAll() => _books;
public Book GetById(int id) =>
_books.FirstOrDefault(b => b.Id == id);
public void Add(Book book) =>
_books.Add(book);
public void Update(Book book)
{
var existing = GetById(book.Id);
if (existing != null)
{
existing.Title = book.Title;
existing.Author = book.Author;
existing.Price = book.Price;
}
}
public void Delete(int id)
{
var book = GetById(id);
if (book != null)
_books.Remove(book);
}
}

Join the conversation! Your thoughts help the community grow.