3.3 Attribute Routing
Same as Web API, Web API OData supports a new type of routing called attribute routing. It uses two Attributes to find controller and action. One is ODataPrefixAttribute
, the other is ODataRouteAttribute
.
You can use attribute routing to define more complex routes and put more control over the routing. Most important, it can extend the coverage of convention routing. For example, you can easily use attribute routing to route the following Uri:
In Web API OData, attribute routing is combined with convention routing by default.
Enabling Attribute Routing
ODataRoutingConventions
provides two methods to register routing conventions:
As the name implies, the first one creates a mutable list of the default OData routing conventions with attribute routing enabled, while the second one only includes convention routing.
In fact, when you call the basic MapODataServiceRoute
, it enables the attribute routing by default as:
However, you can call other version of MapODataServiceRoute
to custom your own routing conventions. For example:
ODataRouteAttribute
ODataRouteAttribute
is an attribute that can, and only can be placed on an action of an OData controller to specify the OData URLs that the action handles.
Here is an example of an action defined using an ODataRouteAttribute
:
With this attribute, Web API OData tries to match the request Uri with Customers({id})/Address/City
routing template to GetCityOfACustomer()
function in MyController
. For example, the following request Uri will invoke GetCityOfACustomer
:
For the above request Uri, id
in the function will have 1
, 2
and 301
value.
However, for the following request Uri, it can’t match to `GetCityOfACustomer()’:
Web API OData supports to put multiple ODataRouteAttribute
on the same OData action. For example,
ODataRoutePrefixAttribute
ODataRoutePrefixAttribute
is an attribute that can, and only can be placed on an OData controller to specify the prefix that will be used for all actions of that controller.
ODataRoutePrefixAttribute
is used to reduce the routing template in ODataRouteAttribute
if all routing template in the controller start with the same prefix. For example:
Then you can use ODataRoutePrefixAttribute
attribute on the controller to set a common prefix.
Now, Web API OData supports to put multiple ODataRoutePrefixAttribute
on the same OData controller. For example,
Route template
The route template is the route combined with ODataRoutePrefixAttribute
and ODataRouteAttribute
. So, for the following example:
The GetAddress
matches to Customers({id})/Address
route template. It’s called key template because there’s a template {id}
. So far in Web API OData, it supports two kind of templates:
- key template, for example:
- function parameter template, for example:
Web API OData team also works to add the third template, that is the dynamic property template. It’s planed to ship in next release.
You can refer to this blog for attribute routing in Web API 2.