Esempio n. 1
0
        private static async Task GenerateCollectionResultValue <T>(ResultExecutingContext context, PageList <T> records, string routName)
        {
            if (!records.Any() || context.Result is not ObjectResult resultFromAction)
            {
                return;
            }

            GeneratePaginationHeader(context, records.TotalCount, records.PageSize, records.CurrentPage, records.TotalPages);

            var linkGen = context.HttpContext.RequestServices.GetRequiredService <LinkGenerator>();

            // Get resource collection parameter
            var    paraDesc  = context.ActionDescriptor.Parameters.FirstOrDefault(t => t.Name.IsCollectionParameter());
            object parameter = null;

            if (paraDesc != null && context.Controller is Controller controller)
            {
                parameter = Activator.CreateInstance(paraDesc.ParameterType);
                if (parameter != null)
                {
                    await controller.TryUpdateModelAsync(parameter, paraDesc.ParameterType, "");
                }
            }

            if (parameter is ResourceCollectionParameters collectionResourcePara)
            {
                var links = records.CreatePaginationLinks(routName, context, linkGen, collectionResourcePara);
                resultFromAction.Value = new { value = records.ShapeData(collectionResourcePara.Fields), links };
            }
        }