Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active July 30, 2016 14:25
Show Gist options
  • Save AlbertoMonteiro/aa0ddadc211d09aff94beecd5b71a29b to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/aa0ddadc211d09aff94beecd5b71a29b to your computer and use it in GitHub Desktop.
Custom EnableQueryAttribute to enable odata $inlinecount
using System;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Filters;
using System.Web.Http.OData;
using static System.Activator;
using static System.Net.HttpStatusCode;
namespace Conecta2.Web.Infraestrutura.Filter
{
public sealed class EnableQueryCustomAttribute : EnableQueryAttribute
{
private static readonly Type PageResultType = typeof(PageResult<>);
private const string MS_INLINECOUNT = "MS_InlineCount";
public override void OnActionExecuted(HttpActionExecutedContext context)
{
IQueryable query;
base.OnActionExecuted(context);
if (context.Response.TryGetContentValue(out query))
{
var request = context.Request;
var properties = request.Properties;
if (properties.ContainsKey(MS_INLINECOUNT))
{
var total = (long?)properties[MS_INLINECOUNT];
context.Response = request.CreateResponse(OK, CreateInstance(PageResultType.MakeGenericType(query.ElementType), query, default(Uri), total));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment