Last active
July 30, 2016 14:25
-
-
Save AlbertoMonteiro/aa0ddadc211d09aff94beecd5b71a29b to your computer and use it in GitHub Desktop.
Custom EnableQueryAttribute to enable odata $inlinecount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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