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/productsYou 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.