ASP.NET Core Web API – Introduction

What is ASP.NET Core Web API?

ASP.NET Core Web API is a framework for building HTTP services that can be accessed from any client, including browsers, mobile devices, and desktop applications. It’s built on top of the .NET platform, offering high performance, cross‑platform support, and a modern development experience.

Prerequisites

Step 1 – Create a New Web API Project

Open a terminal and run the following command:

dotnet new webapi -n WeatherApi

Step 2 – Explore the Project Structure

Key files:

Step 3 – Run the API

Navigate to the project folder and start the server:

cd WeatherApi
dotnet run

Open http://localhost:5000/weatherforecast to see the JSON response.

Step 4 – Add a New Endpoint

Create Controllers/TodoController.cs with the following code:

using Microsoft.AspNetCore.Mvc;

namespace WeatherApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TodoController : ControllerBase
    {
        private static readonly List<TodoItem> Items = new();

        [HttpGet]
        public IEnumerable<TodoItem> Get() => Items;

        [HttpPost]
        public IActionResult Create(TodoItem item)
        {
            Items.Add(item);
            return CreatedAtAction(nameof(Get), new { id = item.Id }, item);
        }
    }

    public record TodoItem(int Id, string Title, bool Completed);
}

After rebuilding, you can POST JSON to /todo with tools like Insomnia or curl.

What’s Next?