【问题标题】:Null Reference Exception occurs when using odata in Web API calls在 Web API 调用中使用 odata 时发生空引用异常
【发布时间】:2012-04-03 11:02:14
【问题描述】:

我有以下产品类:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}

以及以下 Web API 控制器类:

public class ProductController : ApiController
{
    public IQueryable<Product> Get()
    {
        var products = new List<Product>
        {
            new Product {Id = 1, Name = "Beans"},
            new Product {Id = 2, Name = "Sausages"},
            new Product {Id = 3, Name = "Bread"}
        };

        return products.AsQueryable();
    }
}

我正在使用 Fiddler 向它发出请求。这些工作正常:

localhost:49629/API/产品

localhost:49629/API/Product?$orderby=Name

但是当我尝试使用过滤时,比如:

localhost:49629/API/Product?$filter=Id gt 2

出现空引用。调试器中没有可用的源代码,所以我不确定出了什么问题。有人可以帮忙吗?

【问题讨论】:

  • 你“在调试器中没有可用的源代码”,但也许你有一个调用堆栈?

标签: asp.net-mvc odata iqueryable asp.net-web-api


【解决方案1】:

对于 Fiddler,您需要对请求进行编码:

localhost:49629/API/Product?$filter=Id%20gt%202

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多