Attribute routing
In Chapter 1, Getting Started with the ASP.NET Web API, we discussed HttpRoutingDispatcher
and how we can define a route template in the WebApiConfig.cs
file. This method is referred to as "convention-based routing". It allows developers to define multiple route templates at a global configuration level for all controllers. Every incoming request is then matched to one of these routes to determine the controller and action that will process the request. If no paths match, an HTTP 404 Not Found
error is issued. Convention-based routing works great for simplistic route matching. However, it becomes cumbersome to maintain a routing table, for example, in situations where we may want to provide nested URI matching. Also, REST architecture constraints recommend having unique explicit paths for recognition of any single resource. Starting with ASP.NET Web API 2, the attribute routing scheme was introduced to tackle such situations.
Attribute routing enables a more...