一、配置项

1、WebApiConfig.cs添加如下代码:

//  api 支持 cors允许Ajax发起跨域的请求(nuget 中搜索 ASP.NET Cross-Origin Support,然后安装)var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*");config.EnableCors(cors);// api 支持 odata 查询( nuget 中搜索 ASP.NET Web API OData)System.Web.Http.OData.Extensions.HttpConfigurationExtensions.AddODataQueryFilter(config);

2、Global.asax.cs添加如下代码:

//添加 ASP.NET Web API 2 的 全部集合,注意这个要放在前面
 GlobalConfiguration.Configure(WebApiConfig.Register);RouteConfig.RegisterRoutes(RouteTable.Routes);

二、使用方法

ODataController.cs

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
//[EnableQuery(MaxTop = 100)] // 指定 top 参数的最大值为 100
//[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Skip | AllowedQueryOptions.Top)]
public IQueryable<Sites> Get()
{List<Sites> siteslist = ...;return siteslist.AsQueryable();
}

三、查询示例:

1、 查询前10条数据,并返回Id值小于10的数据集合:

http://localhost:49636/api/odata?$top=10&$filter=Id lt 10

2、查询ParnetId等于6551的数据人集合:

http://localhost:49636/api/app?$filter=ParentId eq 6551

3、上面的集合按照Code倒序排列:

http://localhost:49636/api/app?$filter=ParentId eq 6551&$orderby=Code desc

4、只返回Name,Id属性的集合:

http://localhost:49636/api/app?$format=json&$select=Name,Id

运算符 描述 示例

eq

等于

/AccountSet?$filter=Address1_City eq 'Redmond'

ne

不等于

/AccountSet?$filter=Address1_City ne null

gt

大于

/AccountSet?$filter=CreditLimit/Value gt 1000

ge

大于或等于

/AccountSet?&$filter=CreditLimit/Value ge 1000

Lt

小于

/AccountSet?$filter=CreditLimit/Value lt 1000

le

小于或等于

/AccountSet?$filter=CreditLimit/Value le 1000

and

逻辑与

/AccountSet?$filter=CreditLimit/Value ge 1000 and Address1_StateOrProvince eq 'TX'

or

逻辑或

/AccountSet?$filter=AccountCategoryCode/Value eq 2 or AccountRatingCode/Value eq 1

not

逻辑非

/AccountSet?$filter=(AccountCategoryCode/Value ne null) and not (AccountCategoryCode/Value eq 1)

选项 说明

$expand

指示应在所检索的记录或集合中检索相关记录。

$filter

指定为在集合中返回记录计算结果必须为 true 的表达式或函数。

$orderby

确定使用哪些值对记录集合进行排序。

$select

指定要返回的属性子集。

$skip

设置在集合中检索记录之前要跳过的记录数。

$top

确定要返回的最大记录数。

相关教程:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api

The Service:

It all starts with a Data Service hosted somewhere:

http://server/service.svc

Basic queries:

You access the Data Service entities through resource sets, like this:

http://server/service.svc/People

You request a specific entity using its key like this:

http://server/service.svc/People(16)

Or by using a reference relationship to something else you know:

http://server/service.svc/People(16)/Mother

This asks for person 16’s mother.

Once you have identified an entity you can refer to it’s properties directly:

http://server/service.svc/People(16)/Mother/Firstname

$value:

But the last query wraps the property value in XML, if you want just the raw property value you append $value to the url like this:

http://server/service.svc/People(16)/Mother/Firstname/$value

$filter:

You can filter resource sets using $filter:

http://server/service.svc/People?$filter=Firstname  eq ‘Fred’

Notice that strings in the filter are single quoted.

Numbers need no quotes though:

http://server/service.svc/Posts?$filter=AuthorId eq 1

To filter by date you have identity the date in the filter, like this:

http://server/service.svc/Posts?$filter=CreatedDate eq DateTime’2009-10-31′

You can filter via reference relationships:

http://server/service.svc/People?$filter=Mother/Firstname eq ‘Wendy’

The basic operators you can use in a filter are:

Operator

Description

C# equivalent

eq

equals

==

ne

not equal

!=

gt

greater than

>

ge

greater than or equal

>=

lt

less than

<

le

less than or equal

<=

and

and

&&

or

or

||

()

grouping

()

There are also a series of functions that you can use in your filters if needed.

$expand:

If you want to include related items in the results you use $expand like this:

http://server/service.svc/Blogs?$expand=Posts

This returns the matching Blogs and each Blog’s posts.

$select:

Some Data Services allow you to limit the results to just the properties you require – aka projection – for example if you just want the Id and Title of matching Posts you would need something like this:

http://server/service.svc/Posts?$select=Id,Title

You can even project properties of related objects too, like this:

http://server/service.svc/Posts?$expand=Blog&$select=Id,Title,Blog/Name

This projects just the Id, Title and the Name of the Blog for each Post.

$count:

If you just want to know how many records would be returned, without retrieving them you need $count:

http://server/service.svc/Blogs/$count

Notice that $count becomes one of the segments of the URL – it is not part of the query string – so if you want to combine it with another operation like $filter you have to specify $count first, like this:

http://server/service.svc/Posts/$count?$filter=AuthorId eq 6

This query returns the number of posts authored by person 6.

$orderby:

If you need your results ordered you can use $orderby:

http://server/service.svc/Blogs?$orderby=Name

Which returns the results in ascending order, to do descending order you need:

http://server/service.svc/Blogs?$orderby=Name%20desc

To filter by first by one property and then by another you need:

http://server/service.svc/People?$orderby=Surname,Firstname

Which you can combine with desc if necessary.

$top:

If you want just the first 10 items you use $top like this:

http://server/service.svc/People?$top=10

$skip:

If you are only interested in certain page of date, you need $top and $skip together:

http://server/service.svc/People?$top=10&$skip=20

This tells the Data Service to skip the first 20 matches and return the next 10. Useful if you need to display the 3rd page of results when there are 10 items per page.

Note: It is often a good idea to combine $top & $skip with $orderby too, to guarantee the order results are retrieved from the underlying data source is consistent.

$inlinecount & $skiptoken:

Using $top and $skip allows the client to control paging.

But the server also needs a way to control paging – to minimize workload need to service both naive and malicious clients – the OData protocol supports this via Server Driven Paging.

With Server Driven Paging turned on the client might ask for every record, but they will only be given one page of results.

This as you can imagine can make life a little tricky for client application developers.

If the client needs to know how many results there really are, they can append the $inlinecount option to the query, like this:

http://server/service.svc/People?$inlinecount=allpages

The results will include a total count ‘inline’, and a url generated by the server to get the next page of results.

This generated url includes a $skiptoken, that is the equivalent of a cursor or bookmark, that instructs the server where to resume:

http://server/service.svc/People?$skiptoken=4

$links

Sometime you just need to get the urls for entities related to a particular entity, which is where $links comes in:

http://server/service.svc/Blogs(1)/$links/Posts

This tells the Data Service to return links – aka urls – for all the Posts related to Blog 1.

$metadata

If you need to know what model an OData compliant Data Service exposes, you can do this by going to the root of the service and appending $metadata like this:

http://server/service.svc/$metadata

This should return an EDMX file containing the conceptual model (aka EDM) exposed by the Data Service.

转载于:https://www.cnblogs.com/firstcsharp/p/5404194.html

请MVC5 WebApi2 支持OData协议查询相关推荐

  1. 我使用Asp.net MVC WebAPI支持OData协议进行分页操作的笔记(第一篇)

    OData协议.多么牛B的技术. 传统的分页写习惯了,最近项目中,用到了 Asp.net WebAPI 2.0来做数据交互接口.至于为什么要使用WebAPI,我想只要是对OData协议有了解的朋友.只 ...

  2. 使用OData协议查询Windows日志

    OData开放数据协议是微软针对Google的GData推出的,旨在推广Web程序数据库格式标准化的开放数据协议,微软将 OData 定义为基于 HTTP.AtomPub 和 JSON 的协议,增强各 ...

  3. 我使用Asp.net MVC WebAPI支持OData协议进行分页操作的笔记(第二篇)

    在阅读这篇文章的时候,我想你已经看完第一篇文章啦·也有可能跟我一样,现在正在使用它Asp.net WebAPI为我们干活儿.可能是服务分页查询接口,也可能是其它操作,遇到了一些小问题.有问题,那咱就来 ...

  4. java odata filter响应,使用 OData 终结点的 OData 系统查询选项

    使用 OData 终结点的 OData 系统查询选项 07/06/2017 本文内容 发布日期: 2016年11月 适用于: Dynamics CRM 2015 您可以使用系统查询选项细化查询结果. ...

  5. OData——让查询变的随心所欲

    OData是什么 Open Data Protocol(开放数据协议,OData)是用来查询和更新数据的一种Web协议,其提供了把存在于应用程序中的数据暴露出来的方式.OData运用且构建于很多Web ...

  6. 微软宣布ASP.NET Core 2.0正式支持OData标准

    近日,OData 团队在微软开发者博客上宣布,ASP.NET Core 2.0 已正式支持 OData 标准,开发者现在可通过包管理器 NuGet 来获取 Microsoft.AspNetCore.O ...

  7. 【转】WCF Data Service 使用小结 (一)—— 了解OData协议

    最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不多,但它确实是个很好的 ...

  8. 基于 cz88 纯真IP数据库开发的 IP 解析服务 - 支持 http 协议请求或 rpc 协议请求,也支持第三方包的方式引入直接使用

    cz88 基于 cz88 纯真IP数据库开发的 IP 解析服务 - 支持 http 协议请求或 rpc 协议请求,也支持第三方包的方式引入直接使用 Go 语言编写 进程内缓存结果,重复的 ip 查询响 ...

  9. 支持jesd204b协议高速DAC芯片AD9144-FMC-EBZ配置笔记

    本文为明德扬原创文章,转载请注明出处! 一.背景 AD9144是一款支持jesd204b协议高速DAC芯片.AD9144-FMC-EBZ是基于AD9144的评估板(Evaluation Board), ...

最新文章

  1. 如何将本地项目上传到自己的GitHub上
  2. Java如何读取JAR包外的properties文件及打成jar包后无法读取到jar包内的properties文件
  3. LAMP架构之编译安装httpd+(php-fpm)+mariadb
  4. 信用经济中的经济因素
  5. c++ 管理员身份_CATIA的管理员模式和多版本环境变量设置
  6. iOS之深入解析内存管理MRC与ARC机制
  7. 多继承完美闪避成员名冲突的问题
  8. android汉字笔顺数据库,汉字笔顺学习(汉字笔顺学习app)V1.80.91009 安卓版
  9. 【论文解析】Intelligent Mediator-based Enhanced Smart Contract for Privacy Protection
  10. Java基础案例 | 第二弹(持续更新...xdm冲啊)
  11. 时间都去哪儿了? 番茄钟告诉你答案
  12. C++ Builder ADO数据库连接与保存
  13. 词向量方法分析《三体》
  14. 苟日新,日日新,又日新
  15. iOS网络资源汇总(值得推荐)
  16. 虚拟机centos安装web服务器
  17. Simulink常用模块库(Integrator/Discrete-TimeIntegrator)
  18. 2021年熔化焊接与热切割试题及解析及熔化焊接与热切割模拟考试题
  19. 新概念英语一册语法总结
  20. 浏览器访问虚拟机中的ElasticSearch

热门文章

  1. 厦门大学数学专业考研试题参考解答
  2. react router v4 简介
  3. (OS 64)指定的网络名不再可用,winnt_accept: Asynchronous AcceptEx failed.
  4. [LeetCode]238.Product of Array Except Self
  5. 关闭 IOS8 最近使用 最近联系人
  6. 如何在Ubuntu下面识别Galaxy Nexus设备
  7. javascript:URL编解码和父子窗口交互
  8. 可变化的鸿蒙武器,DNF2018史诗改版大全 武器套装改版属性介绍
  9. nextcloud icon_吉利ICON的提车日记,这是一个不错的选择
  10. 我如何开始使用Linux