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
- Visual Studio 2022 (or VS Code) with the .NET 8 SDK
- Basic knowledge of C# and HTTP
- Git (optional, for source control)
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:
Program.cs
– Configures the app and middleware.Controllers/WeatherForecastController.cs
– Sample API endpoint.appsettings.json
– Application settings.
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?
- Secure the API with JWT authentication.
- Implement data persistence using Entity Framework Core.
- Write unit and integration tests.
- Deploy to Azure App Service or Docker.