Ejemplo n.º 1
0
        /// <summary>
        /// Invoked as the method executes, and convert the output IQuerable to a paged result set.
        /// </summary>
        /// <param name="context">Context of the request.</param>
        public override void OnActionExecuted(
            ActionExecutedContext context)
        {
            try
            {
                Microsoft.AspNetCore.Http.HttpContext  httpContext  = context.HttpContext;
                Microsoft.AspNetCore.Http.HttpRequest  httpRequest  = httpContext.Request;
                Microsoft.AspNetCore.Http.HttpResponse httpResponse = httpContext.Response;

                if (httpResponse.IsSuccessStatusCode())
                {
                    ObjectResult objectResult = (ObjectResult)context.Result;

                    if (objectResult.Value is not IQueryable queryableValue)
                    {
                        base.OnActionExecuted(context);
                    }
                    else
                    {
                        PagingInfo pagingInfo = PagingInfo.FromRequest(httpRequest);

                        PagedResult pagedResult = queryableValue.ToPagedResult(pagingInfo, httpRequest);

                        if (pagedResult.Pagination.TotalResults > 0)
                        {
                            objectResult.Value = pagedResult;
                        }
                        else
                        {
                            context.Result = new NoContentResult();
                        }
                    }
                }
                else
                {
                    base.OnActionExecuted(context);
                }

                return;
            }