Short tutorial

Build Your First ASP.NET Core Web API

One of the best ways to enter modern backend development with C# is to build a simple Web API. In this tutorial, you will create a small API that returns product data as JSON and learn the basic structure behind controllers, routes, and endpoints.

What you will learn

This tutorial focuses on the essentials so you can get a working result quickly.

Create an ASP.NET Core Web API project Add a controller and route Return JSON data from an endpoint

Step 1: Create a new Web API project

Open Visual Studio and create a new ASP.NET Core Web API project. Name it FirstWebApi. Choose the current .NET version and keep the default API template enabled so you can start quickly.

Step 2: Understand the goal

Instead of rendering a desktop form, a Web API exposes endpoints that other applications can call. A browser, mobile app, JavaScript frontend, or another backend service can send requests and receive JSON responses.

Step 3: Add a simple controller

Create a controller named ProductsController.cs inside the Controllers folder and add this code:

using Microsoft.AspNetCore.Mvc;

namespace FirstWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductsController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetProducts()
        {
            var products = new[]
            {
                new { Id = 1, Name = "Keyboard", Price = 120.00 },
                new { Id = 2, Name = "Mouse", Price = 55.00 },
                new { Id = 3, Name = "Monitor", Price = 899.00 }
            };

            return Ok(products);
        }
    }
}

Step 4: Run the application

Run the project, then open the endpoint:

/api/products

You should see a JSON response containing the sample products.

Step 5: Why this matters

This small example teaches several important ideas at once: routing, controllers, JSON output, and HTTP GET requests. These ideas form the base for more advanced APIs with databases, authentication, and validation.

Try this next: Add a second endpoint that returns one product by ID, or add a POST endpoint that accepts new product data.