加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WebAPI 2.2 OData v4 [EnableQuery]应用在哪里?

发布时间:2020-12-15 06:22:17 所属栏目:百科 来源:网络整理
导读:从2015年1月起应用EnableQueryAttribute哪里正确/不正确? 文件链接如下: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint 说: The [EnableQuery] attribute enables clients to modify the q
从2015年1月起应用EnableQueryAttribute哪里正确/不正确?

文件链接如下:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

说:

The [EnableQuery] attribute enables clients to modify the query,by using query options such as $filter,$sort,and $page. For more information,see Supporting OData Query Options.

以下链接文件:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

说:

The EnableQuerySupport method enables query options globally for any controller action that returns an IQueryable type.

但是关于WebApi 2.2上的OData 4的这个文档已经将它放在了返回IHttpActionResult的操作上:

http://blogs.msdn.com/b/webdev/archive/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0.aspx

[ODataRoutePrefix("Teams")]
public class TeamsEntitySetController : ODataController
{
    private readonly LeageContext _leage = new LeageContext();

    [EnableQuery]
    [ODataRoute]
    public IHttpActionResult GetFeed()
    {
        return Ok(_leage.Teams);
    }
    [ODataRoute("({id})")]
    [EnableQuery]
    public IHttpActionResult GetEntity(int id)
    {
        return Ok(SingleResult.Create<Team>(_leage.Teams.Where(t => t.Id == id)));
    }
}

我会疯狂地尝试在OData v4 / WebApi 2.2上找到最新,准确和一致的文档.

今天是正确的?

解决方法

使用全局配置(HttpConfiguration对象的实例)并调用
config.Filters.Add(new EnableQueryAttribute()
            {
                PageSize = 2
                // .. other settings
            });

这个工作

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读