Microsoft Updates Attribute Routing In OData ASP.NET Core OData 8.0 RC

Attribute routing lets you achieve more routings by constructing basic and advanced OData routing templates.

How a Web API matches the incoming HTTP requests to an action based on route template attributes decorated on controller or action is known as Attribute routing. ASP.NET Core have a set of route template attributes such as RouteAttribute, HttpGetAttribute to enable attribute routing. Now, ASP.NET Core OData 8.0 RC supports these attributes to help you to define OData attribute routing endpoints.

Earlier ASP.NET Core OData versions used two attributes to find controller and action: ODataRoutePrefixAttribute, and ODataRouteAttribute. In OData 8.0 RC, these two attributes are gone. Now OData attribute routing is changed to use ASP.NET Core route template attribute classes.

In order to enable OData attribute routing to work, either controller or action should decorate an attribute named ODataRoutingAttribute, which is introduced in RC version to avoid polluting other ASP.NET Core route templates, as OData attribute routing is enabled by default if call AddOData(). If you decorate this attribute on the controller, all actions in that controller are considered as OData actions.

ODataController has ODataRoutingAttribute decorated as below:

 [ODataRouting]
public abstract class ODataController : ControllerBase
{}

To make your own controller and derived it from ODataController is a usual way to use OData attribute routing.

 public class HandleBookController : ODataController
{}

You can also decorate multiple RouteAttribute on the controller. In that case template of each (Route) joins each of the route template of attributes on the action methods.

Users can also make use of [NonODataController] and [NonODataAction] to exclude certain controller or action out of attribute routing.

In summary Attribute routing lets you achieve more routings by constructing basic and advanced OData routing templates. Additionally, you can mix it with conventional routing also to achieve more. 

To learn more you can visit the official announcement here.