ASP .NET MVC 4 WebApi:手动处理 OData 查询 [英] ASP .NET MVC 4 WebApi: Manually handle OData queries

查看:24
本文介绍了ASP .NET MVC 4 WebApi:手动处理 OData 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 ASP .NET MVC 4 提供的 WebAPI 制作的 Web 服务.我知道 WebAPI 工作的层会自动处理 OData 查询(例如 $filter$top$skip),但是如果我想自己处理过滤呢?

I have a Web Service made using the WebAPI provided by ASP .NET MVC 4. I know that the layer on top of which WebAPI works automatically handles OData Queries (such as $filter, $top, $skip), but what if I want to handle the filtering by myself?

我不是简单地从我的数据库返回数据,但我还有另一层它添加了一些属性,进行了一些转换等.所以查询我的所有数据,转换它们并将它们返回到 WebAPI 类以进行 OData 过滤是不够的.这当然非常慢,而且通常是一个糟糕的想法.

I don't simply return data from my database, but I have another layer which adds some properties, makes some conversions etc. So querying ALL of my data, converting them and returning them to the WebAPI class for OData filtering isn't just good enough. It's of course terribly slow, and generally a crappy idea.

那么有没有一种方法可以将 OData 查询参数从我的 WebAPI 入口点传播到我调用以获取和转换数据的函数?

So is there a way to propagate the OData query parameters from my WebAPI entry point to the functions I call to get and convert the data?

例如,对 /api/people?$skip=10&$top=10 的 GET 会在服务器上调用:

For example, a GET to /api/people?$skip=10&$top=10 would call on the server:

public IQueryable<Person> get() {
    return PersonService.get(SomethingAboutCurrentRequest.CurrentOData);
}

PersonService中:

public IQueryable<Person> getPeople(var ODataQueries) {
    IQueryable<ServerSidePerson> serverPeople = from p in dbContext.ServerSidePerson select p;
    // Make the OData queries
    // Skip
    serverPeople = serverPeople.Skip(ODataQueries.Skip);
    // Take
    serverPeople = serverPeople.Take(ODataQueries.Take);
    // And so on
    // ...

    // Then, convert them
    IQueryable<Person> people = Converter.convertPersonList(serverPeople);
    return people;
}

推荐答案

我刚刚偶然发现了这篇旧帖子,我正在添加这个答案,因为现在自己处理 OData 查询非常容易.举个例子:

I just stumbled across this old post and I'm adding this answer as it's now very easy to handle the OData queries yourself. Here's an example:

[HttpGet]
[ActionName("Example")]
public IEnumerable<Poco> GetExample(ODataQueryOptions<Poco> queryOptions)
{
    var data = new Poco[] { 
        new Poco() { id = 1, name = "one", type = "a" },
        new Poco() { id = 2, name = "two", type = "b" },
        new Poco() { id = 3, name = "three", type = "c" }
    };

    var t = new ODataValidationSettings() { MaxTop = 2 };
    queryOptions.Validate(t);

    //this is the method to filter using the OData framework
    //var s = new ODataQuerySettings() { PageSize = 1 };
    //var results = queryOptions.ApplyTo(data.AsQueryable(), s) as IEnumerable<Poco>;

    //or DIY
    var results = data;
    if (queryOptions.Skip != null) 
        results = results.Skip(queryOptions.Skip.Value);
    if (queryOptions.Top != null)
        results = results.Take(queryOptions.Top.Value);

    return results;
}

public class Poco
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
}

这篇关于ASP .NET MVC 4 WebApi:手动处理 OData 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆