“无法支持查询”控制器错误

时间:2013-04-15 17:04:08

标签: c# asp.net-mvc-4 odata asp.net-web-api

我正在处理以下答案:Web API OData Inlinecount not working

这是我的控制器,提供了一些虚拟数据。

[Queryable]
public class ValuesController : ApiController
{
    // GET api/values
    public PageResult<Category> Get(ODataQueryOptions<Category> queryOptions)
    {
        var returnValue = new List<Category>()
        {
            new Category() { Id = 1, Name= "Category 1"},
            new Category() { Id = 2, Name= "Category 2"},
            new Category() { Id = 3, Name= "Category 3"},
        };

        IQueryable results = queryOptions.ApplyTo(returnValue.AsQueryable());

        return new PageResult<Category>(results as IEnumerable<Category>,
            Request.GetNextPageLink(), Request.GetInlineCount());
    }
}

当我尝试通过Fiddler获取时出现以下错误:

  

“动作'获取'控制器'值',返回类型为'System.Web.Http.OData.PageResult`1 [[ODataTest_Solution.DTO.Category,ODataTest Solution,Version = 1.0.0.0,Culture = neutral, PublicKeyToken = null]]'不支持查询。确保返回内容的类型是IEnumerable,IQueryable或任一接口的通用形式。“

如果我改变方法它返回IQueryable,它显然工作得很好。

知道我可能做错了吗?

1 个答案:

答案 0 :(得分:6)

我已经尝试过你的ValuesController,它运行得很好。

您的问题很可能是您在此操作中添加了[Queryable]属性。如果您使用ODataQueryOptions,则无需使用[Queryable],因为您自己应用查询而无需操作过滤器。