Skip to content
Explain to Dev
Explain to Dev

Empowering developers with the knowledge to build, create, and innovate in the software world.

  • Home
  • About
  • Java
  • Python
  • PHP
  • .NET
  • Node.js
  • SQL
  • Privacy Policy
Explain to Dev

Empowering developers with the knowledge to build, create, and innovate in the software world.

How to Create a RESTful API Using ASP.NET Core

etd_admin, July 26, 2025July 26, 2025

Creating a RESTful API is one of the most common tasks for developers working with ASP.NET Core. A RESTful API allows clients to communicate with your server using HTTP requests like GET, POST, PUT, and DELETE. In this article, we’ll walk through how to create a RESTful API using ASP.NET Core, step by step, in a simple and beginner-friendly way.

Step-by-Step Guide to Create a RESTful API Using ASP.NET Core

Let’s build a simple API that manages a list of books.

Create a New ASP.NET Core Web API Project

Using the .NET CLI:

dotnet new webapi -n BookStoreApi
cd BookStoreApi

This command sets up a basic API template with a WeatherForecast example. You can delete the example controller later.

Define a Model

Create a Models folder, and inside it, add a Book.cs file:

namespace BookStoreApi.Models
{
    public class Book
    {
        public int Id { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Author { get; set; } = string.Empty;
        public decimal Price { get; set; }
    }
}

Create a Service or In-Memory Data Store

For simplicity, we’ll use an in-memory list to store our data. Create a new folder called Services and add BookService.cs:

using BookStoreApi.Models;

namespace BookStoreApi.Services
{
    public class BookService
    {
        private readonly List<Book> _books = new()
        {
            new Book { Id = 1, Title = "The Hobbit", Author = "J.R.R. Tolkien", Price = 10.99M },
            new Book { Id = 2, Title = "1984", Author = "George Orwell", Price = 8.99M }
        };

        public List<Book> GetAll() => _books;

        public Book? Get(int id) => _books.FirstOrDefault(b => b.Id == id);

        public void Add(Book book)
        {
            book.Id = _books.Max(b => b.Id) + 1;
            _books.Add(book);
        }

        public void Update(Book book)
        {
            var index = _books.FindIndex(b => b.Id == book.Id);
            if (index != -1)
                _books[index] = book;
        }

        public void Delete(int id)
        {
            var book = Get(id);
            if (book is not null)
                _books.Remove(book);
        }
    }
}

Register the Service in Program.cs

Open Program.cs and add the service to the DI container:

builder.Services.AddSingleton<BookService>();

Create the Controller

Add a new folder Controllers, and inside it, create BooksController.cs:

using Microsoft.AspNetCore.Mvc;
using BookStoreApi.Models;
using BookStoreApi.Services;

namespace BookStoreApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BooksController : ControllerBase
    {
        private readonly BookService _bookService;

        public BooksController(BookService bookService)
        {
            _bookService = bookService;
        }

        [HttpGet]
        public ActionResult<List<Book>> GetAll() => _bookService.GetAll();

        [HttpGet("{id}")]
        public ActionResult<Book> Get(int id)
        {
            var book = _bookService.Get(id);
            return book is null ? NotFound() : Ok(book);
        }

        [HttpPost]
        public IActionResult Create(Book book)
        {
            _bookService.Add(book);
            return CreatedAtAction(nameof(Get), new { id = book.Id }, book);
        }

        [HttpPut("{id}")]
        public IActionResult Update(int id, Book book)
        {
            if (id != book.Id)
                return BadRequest();

            var existing = _bookService.Get(id);
            if (existing is null)
                return NotFound();

            _bookService.Update(book);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            var book = _bookService.Get(id);
            if (book is null)
                return NotFound();

            _bookService.Delete(id);
            return NoContent();
        }
    }
}

Run and Test the API

You can run the app using:

dotnet run

Once the app is running, test your endpoints using tools like Postman or curl.

Example GET request:

GET http://localhost:5000/api/books

This is how you can create a RESTful API using ASP.NET Core without needing a full database setup. In real-world scenarios, you’d typically connect to a database using Entity Framework Core, but this example keeps things light and easy to understand.

Once you get the basics right, you can start adding features like:

  • Authentication and authorization (e.g., JWT)
  • Swagger/OpenAPI documentation
  • Entity Framework for persistent data
  • Validation and error handling

By following these steps, you’ve learned how to create a RESTful API using ASP.NET Core from scratch, structured your project neatly, and implemented all the basic HTTP methods. You can now build your own APIs and expand them as needed.

If you’re just getting started, practice building more endpoints and try integrating a real database to take your API development further. With ASP.NET Core, it’s efficient and powerful to create a RESTful API using ASP.NET Core for any modern web or mobile application backend.

.NET C# .NETASP .NETRESTful API

Post navigation

Previous post
Next post
©2025 Explain to Dev | WordPress Theme by SuperbThemes