Parameterizing the route template

Attribute-based routing supports a few predefined tokens that are placed in square brackets ([ and ]), and will be replaced at runtime with their corresponding value:

  • [controller]: This will be replaced with the controller name.
  • [action]: This will be replaced with the method name.
  • [area]: If your application supports areas, this will be replaced with the area in which the controller resides. Area functionality is not covered in this book, but for more information, you can refer to https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas.

For example, instead of writing ProductsController explicitly in the RouteAttribute, we can write it like this:

[Route("api/[controller]")][ApiController]

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.