An alternative to adding routes to a routing table is using routing attributes. Routing attributes existed before ASP.NET Core and were even around in ASP.NET MVC and Web API. If we want to have routing attributes automatically recognized by ASP.NET Core, we need to do this:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
In the following sections, we will learn about a few routing attributes and see how to apply them.
Let's see how we can define routes with attributes.
Defining routes
These attributes are used to define routes and can be composed together; if we add a routing attribute to a class and another to one of its methods, the actual route will result from both of them.
The most obvious use of routing attributes would be to decorate an action method, as follows:
[Route("Home/Index")]
public IActionResult Index() { ... }
If, for example, you have many...