jeudi 17 mai 2018

Retrieve original object from ObjectContent in DelegatingHandler

I have the following code which works for paging.

I want to pull the method SetupPagingLinks into a handler which is executed after the controller returns it's result:

Controller and paging info:

[Route("v1/endpoint")]
[HttpGet]
public async Task<IHttpActionResult> GetConnectionsAsync([FromUri]int page = 1,
    [FromUri]int pageSize = 20, [FromUri]string orderBy = nameof(ConsumerConnectionGetDto), [FromUri]bool ascending = true)
{
    var id = await AuthorizationService.GetAzureObjectIdentifierAsync();

    PagedResults result = await ConnectionService.GetPagedResultAsync(id, page, pageSize, orderBy, ascending);

    connections.PagingMetaData.PageLinkInfo = SetupPagingLinks(Url, page, pageSize, result.PagingMetaData.TotalNumberOfPages);

    return Content(HttpStatusCode.OK, result);
}

private PageLinkInfo SetupPagingLinks(UrlHelper url, int page, int pageSize, int totalPages)
{
    PageLinkInfo linkInfo = new PageLinkInfo(); 

    var qs = Url.Request.RequestUri.ParseQueryString();

    //Set default params if missing
    if (qs["page"] == null)
    {
        qs.Set("page", page.ToString());
    }
    if (qs["pageSize"] == null)
    {
        qs.Set("pageSize", pageSize.ToString());
    }

    var baseUrl = Url.Request.RequestUri.GetLeftPart(UriPartial.Path);
    linkInfo.CurrentPageUrl = $"{baseUrl}?{qs}";

    //First page
    qs.Set("page", "1");
    linkInfo.FirstPageUrl = $"{baseUrl}?{qs}";

    //Last page
    qs.Set("page", $"{totalPages}");
    linkInfo.LastPageUrl = $"{baseUrl}?{qs}";

    //Next page
    if (page + 1 <= totalPages)
    {
        qs.Set("page", $"{page + 1}");
        linkInfo.NextPageUrl = $"{baseUrl}?{qs}";
    }
    else
    {
        linkInfo.NextPageUrl = null;
    }

    //Previous page
    if (page - 1 >= 1)
    {
        qs.Set("page", $"{page - 1}");
        linkInfo.PreviousPageUrl = $"{baseUrl}?{qs}";
    }
    else
    {
        linkInfo.PreviousPageUrl = null;
    }

    return linkInfo;
}

Classes:

public class PagedResults<T>
{
    public PagingMetaData PagingMetaData { get; set; }

    public IEnumerable<T> Results { get; set; }
}

public class PagingMetaData
{
    public int CurrentPageNumber { get; set; }

    public int PageSize { get; set; }

    public int TotalNumberOfPages { get; set; }

    public int TotalNumberOfRecords { get; set; }

    public PageLinkInfo PageLinkInfo { get; set; }
}

public class PageLinkInfo
{
    public string NextPageUrl { get; set; }
    public string PreviousPageUrl { get; set; }
    public string FirstPageUrl { get; set; }
    public string LastPageUrl { get; set; }
    public string CurrentPageUrl { get; set; }
}

What I'd like to do is the following:

public class PagingHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {

        var response = await base.SendAsync(request, cancellationToken);

        if (response.Content != null)
        {
            // If (response.Content.getType() == PagedResults)
            // PagedResult<object> myResult =  response.Content as PagedResult<object>;
            // myResult.PagingMetaData.PageLinkInfo = SetupPagingLinks(request, myResult);
        }

        return response;
    }
}

The problem I'm currently facing is that I can't seem to extract the PagedResults object from the response.Content. I can see in the debugger that my PagedResults object is inside response.Content.Value





Aucun commentaire:

Enregistrer un commentaire