Skip to main content

Controllers

Controllers are the classes that handle HTTP Requests.Requests by holding method that correspond to API endpoints. First, create a Controllers folder to store you controllers. To create a controller, create a class with the following naming convention : the name of the resource, starting with a uppercase letter, then Controller.

Then, the class must extend the ControllerBase class and be annotated with the [ApiController] and [Route] annotation :

  • [ApiController] enables automatic model validation, we'ill come to that later
  • [Route] maps the controllers Route, takes a string parameter to map the route

Example :

namespace TodoAPI.Controllers {
  [ApiController]
  [Route("api/[controller]")]
  public class TodosController : ControllerBase {
  }
  
}

This controller handles request that start with : https://mytodo.io/api/Todos