2

在返回 PageResult 的 odata webapi 调用中,我从方法参数中提取 requestUri,操作过滤条件,然后使用新的 uri 构造一个新的 ODataQueryOptions 对象。

(PageResult 方法基于这篇文章: http ://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options )

这是包含 %24inlinecount=allpages 的原始入站 uri

http://localhost:59459/api/apiOrders/?%24filter=OrderStatusName+eq+'Started'&filterLogic=AND&%24skip=0&%24top=10&%24inlinecount=allpages&_=1376341370337

除了 Request.GetInLineCount 返回 null 之外,就返回的数据而言,一切正常。

由于客户端 ui 元素不知道记录的总数,因此在客户端“杀死”分页。

我构造新的 ODataQueryOptions 对象的方式一定有问题。

请在下面查看我的代码。任何帮助,将不胜感激。

我怀疑这篇文章可能包含一些线索https://stackoverflow.com/a/16361875/1433194但我很难过。

public PageResult<OrderVm> Get(ODataQueryOptions<OrderVm> options)
    {

        var incomingUri = options.Request.RequestUri.AbsoluteUri;

//manipulate the uri here to suit the entity model   
//(related to a transformation needed for enumerable type OrderStatusId )
//e.g. the query string may include %24filter=OrderStatusName+eq+'Started' 
//I manipulate this to %24filter=OrderStatusId+eq+'Started'

        ODataQueryOptions<OrderVm> options2;

        var newUri = incomingUri;  //pretend it was manipulated as above

        //Reconstruct the ODataQueryOptions with the modified Uri

        var request = new HttpRequestMessage(HttpMethod.Get, newUri);

        //construct a new options object using the new request object
        options2 = new ODataQueryOptions<OrderVm>(options.Context, request);

        //Extract a queryable from the repository.  contents is an IQueryable<Order>
        var contents = _unitOfWork.OrderRepository.Get(null, o => o.OrderByDescending(c => c.OrderId), "");

        //project it onto the view model to be used in a grid for display purposes
        //the following projections etc work fine and do not interfere with GetInlineCount if
        //I avoid the step of constructing and using a new options object
        var ds = contents.Select(o => new OrderVm
        {
            OrderId = o.OrderId,
            OrderCode = o.OrderCode,
            CustomerId = o.CustomerId,
            AmountCharged = o.AmountCharged,
            CustomerName = o.Customer.FirstName + " " + o.Customer.LastName,
            Donation = o.Donation,
            OrderDate = o.OrderDate,
            OrderStatusId = o.StatusId,
            OrderStatusName = ""
        });

        //note the use of 'options2' here replacing the original 'options'
        var settings = new ODataQuerySettings()
        {
            PageSize = options2.Top != null ? options2.Top.Value : 5
        };

        //apply the odata transformation
        //note the use of 'options2' here replacing the original 'options'    
        IQueryable results = options2.ApplyTo(ds, settings);

        //Update the field containing the string representation of the enum
        foreach (OrderVm row in results)
        {
            row.OrderStatusName = row.OrderStatusId.ToString();
        }

        //get the total number of records in the result set 
        //THIS RETURNS NULL WHEN USING the 'options2' object - THIS IS MY PROBLEM
        var count = Request.GetInlineCount();

        //create the PageResult object
        var pr = new PageResult<OrderVm>(
            results as IEnumerable<OrderVm>,
            Request.GetNextPageLink(),
            count
            );
        return pr;
    }

编辑
所以更正的代码应该是

//create the PageResult object
var pr = new PageResult<OrderVm>(
    results as IEnumerable<OrderVm>,
    request.GetNextPageLink(),
    request.GetInlineCount();
    );
return pr;

编辑
通过将 Json 转换应用于 OrderVm 类的 OrderStatusId 属性(枚举),避免了在控制器方法中对枚举进行字符串转换的需要

[JsonConverter(typeof(StringEnumConverter))]
public OrderStatus OrderStatusId { get; set; }

这消除了 foreach 循环。

4

1 回答 1

0

只有当客户端通过$inlinecount查询选项请求它时,才会出现 InlineCount 。

在您的修改 uri 逻辑中添加查询选项$inlinecount=allpages(如果它尚不存在)。

此外,您的代码中有一个小错误。您正在创建的新 ODataQueryOptions 使用新的requestwhere 就像在 GetInlineCount 调用中一样,您正在使用旧的Request. 他们不一样。

它应该是,

var count = request.GetInlineCount(); // use the new request that your created, as that is what you applied the query to.
于 2013-08-12T22:00:32.407 回答