到目前为止,我有一个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()
});
}
这就像一个魅力,它很好。但是,我最近被告知要将响应元数据(即Total
,Count
和Last
属性)作为响应自定义标头而不是响应正文发送
我无法从ApiController访问Response
。我想到了一个过滤器或属性,但我如何获取元数据值?
我可以在响应中保留所有这些信息,然后有一个过滤器,在将响应发送到客户端之前对其进行反序列化,并创建一个带有标题的新响应,但这看起来很麻烦和糟糕。
有没有办法在ApiController
上直接从此方法添加自定义标题?
答案 0 :(得分:21)
您可以在类似的方法中显式添加自定义标题:
[HttpGet]
[Route("home/students")]
public HttpResponseMessage GetStudents()
{
// Get students from Database
// Create the response
var response = Request.CreateResponse(HttpStatusCode.OK, studends);
// Set headers for paging
response.Headers.Add("X-Students-Total-Count", studends.Count());
return response;
}
有关详细信息,请阅读以下文章:http://www.jerriepelser.com/blog/paging-in-aspnet-webapi-http-headers/
答案 1 :(得分:17)
我已经输入了评论,这是我的完整答案。
您需要创建自定义过滤器并将其应用于控制器。
public class CustomHeaderFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var count = actionExecutedContext.Request.Properties["Count"];
actionExecutedContext.Response.Content.Headers.Add("totalHeader", count);
}
}
在您的控制器
中 public class AddressController : ApiController
{
public async Task<Address> Get()
{
Request.Properties["Count"] = "123";
}
}
答案 2 :(得分:5)
您需要的是:
public async Task<IHttpActionResult> Get()
{
var response = Request.CreateResponse();
response.Headers.Add("Lorem", "ipsum");
return base.ResponseMessage(response);
}
我希望这能回答你的问题。
答案 3 :(得分:4)
简单的解决方案是:
HttpContext.Current.Response.Headers.Add("MaxRecords", "1000");
答案 4 :(得分:4)
或者,如果您需要在每个响应中执行此操作,则最好利用DelegatingHandler。因为它将在请求/响应管道上运行,而不在控制器/操作级别上运行。就我而言,我必须在每个响应中添加一些标头,因此我做了我描述的事情。参见下面的代码段
public class Interceptor : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
response.Headers.Add("Access-Control-Allow-Origin", "*");
response.Headers.Add("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,PUT,OPTIONS");
response.Headers.Add("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token, content-type");
return response;
}
}
您将需要在WebApiConfig中添加此处理程序
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MessageHandlers.Add(new Interceptor());
}
}
答案 5 :(得分:0)
您可以使用自定义ActionFilter,它允许您发送自定义标头并访问HttpContext:
public class AddCustomHeaderFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
actionExecutedContext.Response.Content.Headers.Add("name", "value");
}
}