Attribute-based routing

Attribute-based routing allows you to control the exact route that each controller and action takes part in by using the attributes that decorate your controllers and methods. I recommend that you use this approach for most of your APIs, since it will make your code more explicit and reduce routing errors that might be introduced as you add more controllers and actions.

At the beginning of this chapter, we created the ProductsController, which looked like this:

[Route("api/Products")][ApiController]public class ProductsController : Controller{    ...   }

The RouteAttribute attribute that decorates the ProductController contains the URL template that maps to this controller. In this case, every request with a URL prefixed ...

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.