It’s easy to custom your own routing convention to override the default Web API OData routing convention. Let’s see how to target it.
Property access routing convention
From built-in routing convention section, we know that users should add many actions for every property access.
For example, if the client issues the following property access request Uris:
Service should have the following actions in CustomersController to handle:
If Customer has hundreds of properties, users should add hundres of similar functions in CustomersController. It’s boring and we can create our own routing convention to override it.
Custom routing convention
We can create our own routing convention class by implementing the IODataRoutingConvention. However, if you don’t want to change the behaviour to find the controller, the new added routing convention class can derive from `NavigationSourceRoutingConvention’.
Let’s build a sample property access routing convention class derived from NavigationSourceRoutingConvention.
Where, we routes the following path templates to a certain action named GetProperty.
Enable customized routing convention
The following sample codes are used to enable the customized routing convention:
Where, we insert our own routing convention at the starting position to override the default Web API OData property access routing convention.
Add actions
In the CustomersController, only one method named GetProperty should be added.