Introduction: REST API in C# — It's Simple
\nREST API (Representational State Transfer) is an architectural style for creating web services that is used today in virtually every modern application. If you want to quickly learn how to write backend in C#, then ASP.NET Core is the best choice to start. In this article, we will create a full-fledged REST API for managing a task list (Todo List) in 30 minutes. You will learn how to set up a project, write controllers, connect a database, and test the API via Swagger.
\nReady? Let's go!
\n\nStep 1: Creating the Project and Setting Up the Environment
\nFirst, we need an installed .NET SDK (version 6.0 or newer). You can download it from the official Microsoft website. After installation, open a terminal or command prompt and create a new project:
\ndotnet new webapi -n TodoApi\ncd TodoApi\nThis command will create a web API template with Swagger pre-installed. Open the project in Visual Studio Code or any other IDE. The project structure already includes the Controllers and Models folders and the Program.cs file.
Now let's add support for Entity Framework Core to work with the database. Install the necessary packages:
\ndotnet add package Microsoft.EntityFrameworkCore.InMemory\ndotnet add package Microsoft.EntityFrameworkCore.Design\nWe use an InMemory database for simplicity — data will be stored in RAM. For production, you can replace it with SQL Server or PostgreSQL.
\n\nStep 2: Creating the Data Model and Database Context
\nLet's create the TodoItem model. In the Models folder, create a file TodoItem.cs:
namespace TodoApi.Models;\n\npublic class TodoItem\n{\n public long Id { get; set; }\n public string? Title { get; set; }\n public bool IsComplete { get; set; }\n}\nNow create the database context. In the Data folder (create it if it doesn't exist), add a file TodoContext.cs:
using Microsoft.EntityFrameworkCore;\nusing TodoApi.Models;\n\nnamespace TodoApi.Data;\n\npublic class TodoContext : DbContext\n{\n public TodoContext(DbContextOptions<TodoContext> options) : base(options) { }\n public DbSet<TodoItem> TodoItems { get; set; }\n}\nRegister the context in Program.cs:
using Microsoft.EntityFrameworkCore;\nusing TodoApi.Data;\n\nvar builder = WebApplication.CreateBuilder(args);\n\nbuilder.Services.AddDbContext<TodoContext>(opt =>\n opt.UseInMemoryDatabase("TodoList"));\n\nbuilder.Services.AddControllers();\nbuilder.Services.AddEndpointsApiExplorer();\nbuilder.Services.AddSwaggerGen();\n\nvar app = builder.Build();\n\nif (app.Environment.IsDevelopment())\n{\n app.UseSwagger();\n app.UseSwaggerUI();\n}\n\napp.UseHttpsRedirection();\napp.UseAuthorization();\napp.MapControllers();\napp.Run();\n\nStep 3: Writing the Controller with CRUD Operations
\nLet's create a controller for working with tasks. In the Controllers folder, add a file TodoItemsController.cs:
using Microsoft.AspNetCore.Mvc;\nusing Microsoft.EntityFrameworkCore;\nusing TodoApi.Data;\nusing TodoApi.Models;\n\nnamespace TodoApi.Controllers;\n\n[Route("api/[controller]")]\n[ApiController]\npublic class TodoItemsController : ControllerBase\n{\n private readonly TodoContext _context;\n\n public TodoItemsController(TodoContext context)\n {\n _context = context;\n }\n\n // GET: api/TodoItems\n [HttpGet]\n public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()\n {\n return await _context.TodoItems.ToListAsync();\n }\n\n // GET: api/TodoItems/5\n [HttpGet("{id}")]\n public async Task<ActionResult<TodoItem>> GetTodoItem(long id)\n {\n var todoItem = await