An action method is a controller method that handles an HTTP request and produces an HTTP response. Related actions live in a controller, where shared routing, services, authorization, caching, and filters can be configured once. Web APIs commonly inherit from ControllerBase and return data such as JSON or XML rather than views. section 1.2.3
Create a controller, add routing and HTTP-method attributes, then implement the method that returns the required result. In a board-game API, BoardGamesController uses [Route("[controller]")], [ApiController], and [HttpGet(Name = "GetBoardGames")] to expose /BoardGames and return an array of BoardGame objects. section 2.4.5
Important conventions include:
- Use [HttpGet], [HttpDelete], or another HTTP attribute to constrain which request method reaches an action. section 1.2.3 section 3.4.3
- Add route templates such as [HttpGet("{id}")] when an action receives a value from the URL. section 1.2.3
- Return framework results such as ContentResult when the endpoint must send response text, and preserve required CORS and caching settings. section 3.4.3
- Use controller boundaries to group related endpoints; OpenAPI tooling can then group their actions under controller-based tags. section 11.2.7
- Use an action as a wrapper when it delegates work to another client, such as a gRPC client, and apply authorization attributes to restricted operations. section 10.5.4
[ApiController] #1
[Route("api/[controller]")] #2
public class SampleController : ControllerBase
{
public SampleController()
{
}
[HttpGet] #3
public string Get()
{
return "TODO: return all items";
}
[HttpGet("{id}")] #4
public string Get(int id)
{
return $"TODO: return the item with id #{id}";
}
[HttpDelete("{id}")] #5
public string Delete(int id)
{
return $"TODO: delete the item with id #{id}";
}
}
Use this compact controller example to inspect route prefixes, HTTP-method attributes, identifier parameters, and ControllerBase inheritance together. section 1.2.3
Board-game collection endpoint
A GET request to /BoardGames can return three BoardGame objects—Axis & Allies, Citadels, and Terraforming Mars—with their identifiers, names, and release years. The same behavior could be implemented with a Minimal API, but a controller remains convenient as data retrieval and updates become more complex. section 2.4.5


