public IActionResult GetApplications( ApplicationsResourceParameters applicationsResourceParameters, // AuthorsResourceParameters => URL Optional Input Parameters for Paging and filtering [FromHeader(Name = "Accept")] string mediaType // HATEOAS and Content Negotiation => Accept : //application/vnd.vaibhav.hateoas+json ) { try { // Check if non existing property has been requested // This is dynamic Mapping of AuthorDto to Entity.Author // e.g. http://localhost:6058/api/applications?orderBy=dateofbirth => In this case => 'dateofbirth' does not exist if (!_propertyMappingService.ValidMappingExistsFor <ApplicationDto, Application> (applicationsResourceParameters.OrderBy)) { return(BadRequest()); } if (!_typeHelperService.TypeHasProperties <ApplicationDto> (applicationsResourceParameters.Fields)) { return(BadRequest()); } var applicationsFromRepo = _applicationInfoRepository.GetApplications(applicationsResourceParameters); var results = Mapper.Map <IEnumerable <ApplicationDto> >(applicationsFromRepo); #region Wrapping HATEOAS (Dynamic Approach) in Content Negotiation => Accept : application/vnd.vaibhav.hateoas+json if (mediaType == "application/vnd.vaibhav.hateoas+json") { var paginationMetadata = new { totalCount = applicationsFromRepo.TotalCount, pageSize = applicationsFromRepo.PageSize, currentPage = applicationsFromRepo.CurrentPage, totalPages = applicationsFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); #region Supporting HATEOAS (Dynamic Approach) var links = CreateLinksForApplications(applicationsResourceParameters, applicationsFromRepo.HasNext, applicationsFromRepo.HasPrevious); var shapedApplications = results.ShapeData(applicationsResourceParameters.Fields); var shapedApplicationsWithLinks = shapedApplications.Select(application => { var applicationsAsDictionary = application as IDictionary <string, object>; var authorLinks = CreateLinksForApplication( (Int32)applicationsAsDictionary["Id"], applicationsResourceParameters.Fields); applicationsAsDictionary.Add("links", authorLinks); return(applicationsAsDictionary); }); var linkedCollectionResource = new { value = shapedApplicationsWithLinks, links = links }; return(Ok(linkedCollectionResource)); #endregion } #endregion #region Accept : application/json else { var previousPageLink = applicationsFromRepo.HasPrevious ? CreateApplicationsResourceUri(applicationsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = applicationsFromRepo.HasNext ? CreateApplicationsResourceUri(applicationsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = applicationsFromRepo.TotalCount, pageSize = applicationsFromRepo.PageSize, currentPage = applicationsFromRepo.CurrentPage, totalPages = applicationsFromRepo.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); return(Ok(results.ShapeData(applicationsResourceParameters.Fields))); } #endregion //return Ok(results.ShapeData(applicationsResourceParameters.Fields)); // Before SHAPING of data //return Ok(results); } catch (Exception e) { _logger.LogInformation(e.Message); throw; } }