Developing Web APIs

ASP.NET Core for RESTful endpoints

Posted by Rodrigo Castro on January 18, 2025

ASP.NET Core is ideal for building RESTful APIs.

🏗️ Create a Web API Project

1
2
3
dotnet new webapi -n MyApiProject
cd MyApiProject
dotnet run

📦 Add a Controller

1
2
3
4
5
6
7
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public IEnumerable<string> Get() => new[] { "Apple", "Banana" };
}

Add this file to the Controllers folder.

🔄 Routing

  • [Route("api/[controller]")] creates /api/products
  • Use [HttpGet], [HttpPost], [HttpPut], [HttpDelete] for verbs

📖 Swagger

  • Included by default for API testing and docs

🛡️ Versioning

  • Use NuGet package Microsoft.AspNetCore.Mvc.Versioning for API version management.

Next: Configuring, publishing, and deploying applications!