I am building an OData Web Service using WebAPI and OData v4.
I was able to get the service to support composite keys through overriding the SelectAction method of the EntityRoutingConvention. However, in previous version of OData this was not needed. I personally believe it’s pretty messy and I feel like I am reinventing the wheel.
Is there any other way?
Use attribute routing.
An example:
Model:
public class Product
{
[Key]
public int ID { get; set; }
[Key]
public string Name { get; set; }
}
Controller method for identifying the entity using composite keys:
[EnableQuery]
[ODataRoute("Products(ID={key1},Name={key2})")]
public IHttpActionResult Get([FromODataUri] int key1, [FromODataUri] string key2)
{
// You business logic for retrieving the entity from your storage using the two keys and return
}
Request sample:
GET http://host/service/Products(ID=1,Name='Car')
No need to override routing convention.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With