public XPaginationHeader( IPaginationMetadata pagination, Func <object, string> urlBuilder, CollectionConfig collectionConfig) { _collectionConfig = collectionConfig; var metadata = new { totalCount = pagination.TotalCount, pageSize = pagination.PageSize, currentPage = pagination.CurrentPage, totalPages = pagination.TotalPages, previousPageUrl = pagination.HasPrevious ? CreatePlayersResourceUri(ResourceUriType.PreviousPage, urlBuilder) : null, nextPageUrl = pagination.HasNext ? CreatePlayersResourceUri(ResourceUriType.NextPage, urlBuilder) : null }; var key = "X-Pagination"; var value = JsonSerializer.Serialize(metadata, new JsonSerializerOptions() { // NOTE: Stops the '?' & '&' chars in the links being escaped Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, IgnoreNullValues = true }); Value = new KeyValuePair <string, StringValues>(key, value); }
/// <summary> /// Factory method to build <see cref="HateoasLink"/>'s for resource collection responses /// </summary> /// <typeparam name="TParameters"></typeparam> /// <param name="parameters"></param> /// <param name="pagination"></param> /// <returns></returns> protected List <HateoasLink> ResourcesGetLinks <TParameters>( TParameters parameters, IPaginationMetadata pagination) { var currentPage = CollectionConfig.Page; var links = new List <HateoasLink> { ResourcesGetLinkBuilder(parameters, HateoasConfig.Relationships.CurrentPage), }; if (pagination.HasPrevious) { CollectionConfig.Page = currentPage - 1; links.Add(ResourcesGetLinkBuilder(parameters, HateoasConfig.Relationships.PreviousPage)); } if (pagination.HasNext) { CollectionConfig.Page = currentPage + 1; links.Add(ResourcesGetLinkBuilder(parameters, HateoasConfig.Relationships.NextPage)); } CollectionConfig.Page = currentPage; return(links); }
private void AddPaginationHeader(string resourcesGetRouteName, [NotNull] IPaginationMetadata pagedEntities) { var xPaginationHeader = new XPaginationHeader( pagedEntities, (parameters) => Url.Link(resourcesGetRouteName, parameters), CollectionConfig ); Response.Headers.Add(xPaginationHeader.Value); }
internal new List <HateoasLink> ResourcesGetLinks <T>(T p, IPaginationMetadata pagination) { return(base.ResourcesGetLinks(p, pagination)); }