ASP.NET Core Web API Interview Question #79

What are route tokens such as [controller] and [action]?

Routing, Versioning, OpenAPI/Swagger & API Documentation Mid-Level Intermediate

Quick Interview Answer

Route tokens are placeholders replaced using controller or action metadata when routes are built. The controller token commonly removes the Controller suffix from the class name.

Detailed Explanation

Route tokens are placeholders replaced using controller or action metadata when routes are built. The controller token commonly removes the Controller suffix from the class name.

Tokens reduce repeated route text, but public API routes should remain intentional because renaming a controller can otherwise change a client-facing URL.

Code Example

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    [HttpGet("[action]")]
    public IActionResult Featured() => Ok();
}

// /api/Products/Featured