ASP.net web api返回空对象列表

时间:2017-06-11 12:45:37

标签: c# asp.net-web-api serialization

我有一个动作,鞋子从DB返回一个有限的商店项目列表,这里的代码如下

    [HttpGet]
    public IEnumerable<ProductInfo> GetTop(int amount = 5)
    {
        var mostPopulardProductIds = dbe.SalesOrderDetail.GroupBy(p => p.ProductID)
            .OrderByDescending(p => p.Count())
            .Take(amount)
            .Select(p => p.Key)
            .ToList();

        var result = dbe.Product
            .Where(p => mostPopulardProductIds.Contains(p.ProductID))
            .Select(p => new ProductInfo()
            {
                Id = p.ProductID,
                Name = p.Name
            }
            ).ToList();
        return result;
    }

这是我的ProductInfo类

[DataContract]
public class ProductInfo
{
    public int Id { get; set; }
    public string Name { get; set; }
    //public
}

但是当我尝试通过浏览器访问它时,我得到了空对象的列表,看起来像第一张图片

但调试器向我显示,该结果变量包含带有数据的普通对象

smlresult

debugger result

我尝试通过Request.CreateResponse方法返回结果,但这仍然不起作用

1 个答案:

答案 0 :(得分:5)

请将DataMember属性添加到类属性,更改ProductInfo类

cards1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v']
def piles(cards1):
    print("You must remember what card is yours!")
    pile = [[],[],[]]
    first = 0
    second = 1
    third = 2
    for i in range (7):
        pile[0].append(cards1[first])
        pile[1].append(cards1[second])
        pile[2].append(cards1[third])
        first += 3
        second += 3
        third += 3
    print(pile)
    return(pile)
piles(cards1)

def sorting_piles():

    sorted_piles = []
    final_pile = []
    which_pile = int(input("Which card is your pile in, 1,2 or 3?"))

    for i in range (2):
        while which_pile not in(1,2,3):
            which_pile = int(input("Invalid input. Which card is your pile in, 1,2 or 3?"))
        if which_pile == (1):
            sorted_piles.append(pile[2,0,1])
        elif which_pile == (2):
            sorted_piles.append(pile[2,1,0])
        else:
            sorted_piles.append(pile[1,2,0])
        print("This is now the new pile:",sorted_piles)

    for i in range(7):
        final_pile.append(sorted_piles[0][i-1])
    for i in range(7):
        final_pile.append(sorted_piles[1][i-1])
    for i in range(7):
        final_pile.append(sorted_piles[2][i-1])
    print("Your card is:",final_pile[10])
    return(final_pile)
sorting_piles()

有关Data Member

的更多信息