How to Write a REST API in C# in 30 Minutes: For Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: REST API in C# — Easier Than It Seems



Developing web applications and microservices is unthinkable without a REST API. If you are just starting your programming journey, creating your own API might seem like a difficult task. However, with the ASP.NET Core framework in C#, you can write a working RESTful service in literally 30 minutes.



In this guide, we will step by step create a simple API for managing a task list (Todo List). You will learn how to set up the project, create a data model, a controller, and test the endpoints. No magic — just clean code and clear explanations.



By the end of the article, you will have a fully functioning API supporting all basic CRUD operations (Create, Read, Update, Delete).



Step 1: Environment Setup and Project Creation



Before writing code, make sure you have the necessary tools installed:



  • .NET SDK (version 8.0 or newer) — download from the official Microsoft website
  • Visual Studio (2022 Community Edition) or Visual Studio Code with the C# extension
  • Postman or any other REST client for testing


Open a terminal (or command line) and run the following command to create a new Web API project:



dotnet new webapi -n TodoApicd TodoApi


After creating the project, open the TodoApi folder in your IDE. The project structure will contain several folders and files, but for now we are interested in three key ones: Program.cs, Controllers, and Models.



Step 2: Creating the Data Model



A model is a class that describes the data structure of our API. For the task list, we need three fields: a unique identifier, a name, and a completion status.



Create a new Models folder in the project root, and inside it, a file TodoItem.cs:



namespace TodoApi.Models;

public class TodoItem{ public int Id { get; set; } public string? Name { get; set; } public bool IsComplete { get; set; }}


Note the question mark after string. It indicates that the Name property can be null. This is good practice to avoid compiler warnings.



Step 3: Creating the Controller and Implementing CRUD



A controller is a class that handles HTTP requests. In ASP.NET Core, controllers inherit from ControllerBase and use attributes for routing.



Create a file TodoItemsController.cs in the Controllers folder:



using Microsoft.AspNetCore.Mvc;using TodoApi.Models;

namespace TodoApi.Controllers;

[ApiController][Route("api/[controller]")]public class TodoItemsController : ControllerBase{ private static List<TodoItem> _todoItems = new();

// GET: api/TodoItems [HttpGet] public ActionResult<IEnumerable<TodoItem>> GetTodoItems() { return _todoItems; }

// GET: api/TodoItems/1 [HttpGet("{id}")] public ActionResult<TodoItem> GetTodoItem(int id) { var item = _todoItems.FirstOrDefault(t => t.Id == id); if (item == null) return NotFound(); return item; }

// POST: api/TodoItems [HttpPost] public ActionResult<TodoItem> PostTodoItem(TodoItem item) { item.Id = _todoItems.Count + 1; _todoItems.Add(item); return CreatedAtAction(nameof(GetTodoItem), new { id = item.Id }, item); }

// PUT: api/TodoItems/1 [HttpPut("{id}")] public IActionResult PutTodoItem(int id, TodoItem updatedItem) { var existingItem = _todoItems.First

Blogs

Book Recommendations