Search code examples
.net-coreodata

.net core how to add Content-range to header


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

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);
        }

Solution

  • Here is what I ended up doing:

      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);
            }