【发布时间】:2021-12-14 23:51:14
【问题描述】:
我正在将 OData 添加到 .Net5 中的现有大型 API,其中每个控制器中的操作方法返回不同的类型(例如 OrganizationFull、OrganizationList 等)
这是可行的,只是响应不包含 OData 元数据,例如 $count。所以当消费者调用 /api/OrganizationList?$count=true 时,它不会得到计数。
我知道要获取此元数据,我必须添加一个 EDM 模型,如下所示:
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddOData(opt => opt.Select().Filter().Count().SetMaxTop(10).AddRouteComponents("odata", GetEdmModel()))
IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<OrganizationFull>(nameof(Organization));
odataBuilder.EntitySet<OrganizationList>(nameof(Organization)); // throws
return odataBuilder.GetEdmModel();
}
这里我指定了 2 个 EntitySet,一个对应于从 OrganizationController 返回的每个实体类型。
但是,这会引发异常“实体集 'Organization' 已配置有不同的 EntityType ('OrganizationFull')。”。如果我删除 OrganizationList 的 EntitySet,当我到达 List 端点时,我会得到 404。
如何让我的端点返回 $count 元数据,同时保留我的 API(从一个控制器返回多个实体类型)?
或者,有没有办法从 OData 基础架构中获取操作方法中的计数?在这种情况下,我可以在响应标头中返回它。
【问题讨论】:
-
有一个相关的问题stackoverflow.com/questions/57616954/…,但它的一个答案在Net5中不起作用。
标签: asp.net-core odata .net-5