对复合键的OData v4 Web API支持

Fra*_*oli 5 composite-key odata asp.net-web-api2

我正在使用WebAPI和OData v4构建OData Web服务.

通过覆盖EntityRoutingConvention的SelectAction方法,我能够获得支持组合键的服务.但是,在以前版本的OData中,这不是必需的.我个人认为它非常混乱,我觉得我正在重新发明轮子.

还有其他方法吗?

Yi *_*SFT 5

使用属性路由.

一个例子:

模型:

public class Product
{
    [Key]
    public int ID { get; set; }

    [Key]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用复合键识别实体的控制器方法:

[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
}
Run Code Online (Sandbox Code Playgroud)

索取样品:

GET http://host/service/Products(ID=1,Name='Car')
Run Code Online (Sandbox Code Playgroud)

无需覆盖路由约定.