问题描述
我不知道如何将 Content-Range 添加到我的 odata 请求的标头中.我的 api 需要一种用于分页的格式:
I'm having no luck find out how to add Content-Range to the header of my odata requests. My api requires a format as such for paging:
Content-Range: posts 0-24/319
我能找到的最接近的是 HTTP 字节范围支持.从这里:https://blogs.msdn.microsoft.com/webdev/2012/11/23/asp-net-web-api-and-http-byte-range-support/.OP 说将写一篇关于 [Queryable] 的帖子,该帖子应该添加对分页的支持,但我还没有看到任何相关信息.
The closest thing I can find is HTTP Byte Range Support. From here:https://blogs.msdn.microsoft.com/webdev/2012/11/23/asp-net-web-api-and-http-byte-range-support/ . The OP says a post will be written about [Queryable] which is supposed to add support for paging, but I have yet to see any info on this.
[EnableQuery]
[ODataRoute]
public IActionResult Get(ODataQueryOptions<HC_PortalActivity>
options)
{
return Ok(Db.HC_PortalActivity_Collection);
}
推荐答案
这是我最终做的:
public static void IncludeContentRange<T>(ODataQueryOptions<T> options, HttpRequest context)
{
var range = options.Request.Query["range"][0].Replace("[", "").Replace("]", "").Split(',');
var q = from x in Db.HC_PortalActivity_Collection
select x;
var headerValue = string.Format("{0} {1}-{2}/{3}", options.Context.NavigationSource.Name.ToLower(), range[0], range[1], q.Count());
context.HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Range");
context.HttpContext.Response.Headers.Add("Content-Range", headerValue);
}
这篇关于.net core 如何将 Content-range 添加到标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!