到目前为止,我有一个GET
方法,如下所示:
protected override async Task<IHttpActionResult> GetAll(QueryData query)
{
// ... Some operations
//LINQ Expression based on the query parameters
Expression<Func<Entity, bool>> queryExpression = BuildQueryExpression(query);
//Begin to count all the entities in the repository
Task<int> countingEntities = repo.CountAsync(queryExpression);
//Reads an entity that will be the page start
Entity start = await repo.ReadAsync(query.Start);
//Reads all the entities starting from the start entity
IEnumerable<Entity> found = await repo.BrowseAllAsync(start, queryExpression);
//Truncates to page size
found = found.Take(query.Size);
//Number of entities returned in response
int count = found.Count();
//Number of total entities (without pagination)
int total = await countingEntities;
return Ok(new {
Total = total,
Count = count,
Last = count > 0 ? GetEntityKey(found.Last()) : default(Key),
Data = found.Select(e => IsResourceOwner(e) ? MapToOwnerDTO(e) : MapToDTO(e)).ToList()
});
}
这很有魅力,很好.然而,我最近被告知将响应metadata(即Total
、Count
和Last
属性)作为响应自定义头而不是响应体发送.
我无法从ApicController访问Response
.我想到了一个过滤器或属性,但如何获取元数据值呢?
我可以在响应中保留所有这些信息,然后有一个过滤器,在发送到客户机之前将响应反序列化,并创建一个带有标题的新过滤器,但这似乎既麻烦又糟糕.
有没有办法直接从这个方法在ApiController
上添加自定义标题?