一、配置项

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.

请MVC5 WebApi2 支持OData协议查询的更多相关文章

  1. 让Asp.net mvc WebAPI 支持OData协议进行分页查询操作

    这是我在用Asp.net mvc WebAPI 支持 OData协议 做分页查询服务时的 个人拙笔. 代码已经开发到oschina上.有兴趣的朋友可以看看,欢迎大家指出不足之处. 看过了园子里的几篇关 ...

  2. 让Asp.Net WebAPI支持OData查询,排序,过滤。

    让Asp.Net WebAPI支持OData后,就能支持在url中直接输入排序,过滤条件了. 一.创建Asp.Net WebAPI项目: 二.使用NuGet安装Asp.Net WebAPI 2.2和O ...

  3. 让Asp.Net WebAPI支持OData查询,排序,过滤。(转)

    出处:http://www.cnblogs.com/liuzhendong/p/4233380.html 让Asp.Net WebAPI支持OData后,就能支持在url中直接输入排序,过滤条件了. ...

  4. AspNet.OData 协议概述

    1. 什么是OData? OData 全称 Open Data Protocol,字面理解为开放数据协议,是一个基于Http协议且API实现为Restful风格的协议标准,目前由微软支持大力推广,你可 ...

  5. WCF Data Service 使用小结 (一)—— 了解OData协议

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

  6. 使用 node-odata 轻松创建基于 OData 协议的 RESTful API

    前言 OData, 相信身为.NET程序员应该不为陌生, 对于他的实现, 之前也有童鞋进行过介绍(见:这里1,这里2). 微软的WCF Data Service即采用的该协议来进行通信, ASP.NE ...

  7. 阿里云全站加速DCDN全面支持WebSocket协议

    WebSocket协议可以为网站和应用提供真正的双向通信,具有控制开销.保持连接状态.更强实时性.更好的压缩效果等优点,是当下低延时应用最常采用的一种技术协议.为了更好的满足客户在实时通讯场景下的加速 ...

  8. 1、大部分社交平台接口不支持https协议。

    参考文献来自:http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85 ...

  9. 精通Dubbo——Dubbo支持的协议的详解

    转: 精通Dubbo——Dubbo支持的协议的详解 2017年06月02日 22:26:57 孙_悟_空 阅读数:44500   Dubbo支持dubbo.rmi.hessian.http.webse ...

随机推荐

  1. GeoIP的使用

    GeoIP介绍: 什么是GepIP ? 所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息.这里面的技术不算难题,关键在于有个精准 的数据库.有了准确的数据源 ...

  2. fpdf中文乱码,添加字符集

    中文乱码 引入Olivier的chinese.php(点击下载) 支持中文,其中有Big5,GB两种 使用方法也很简单 $pdf->AddBig5Font(); $pdf->SetFont ...

  3. IOS定位核心与地图

    IOS定位核心与地图                 Core Location以及Map框架包通常能给我们的应用程序添加定位和地图相关的服务.Core Location框架包通常是使用硬件设备来进行 ...

  4. uva 1203 - Argus(优先队列)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3644" target="_blank ...

  5. 关于MORMOT跨平台

    关于MORMOT跨平台 MORMOT服务端程序,支持Win32 / Win64.还有LINUX,通过FPC. 但是你能够写一个客户端在所有DELPHI支持的平台,要使用 cross-platform ...

  6. 关于comparator接口和comparable接口以及它们各自的方法compare()和compareTo()

    在今天做的LeetCode的题中有两道都出现了利用接口实现对象的排序.两题的相关链接: 1.利用comparable接口对对象排序 2.利用comparator接口实现排序 因为之前都没接触过这两个接 ...

  7. 代码规范审查 – Sonar分析项目

    Sonar搭建成功之后,就可以通过简单的CMD指令进行项目分析,此篇主要介绍使用Sonar来分析.net项目. 扫描步骤: a.    打开CMD,切换到指定的项目根目录,和.sln同级目录即可(此处 ...

  8. Linux内核的idle进程分析

    1. idle是什么 简单的说idle是一个进程,其pid号为 0.其前身是系统创建的第一个进程.也是唯一一个没有通过fork()产生的进程. 在smp系统中,每一个处理器单元有独立的一个执行队列,而 ...

  9. java.io.FileNotFoundException: ***(Too many open files)

    http://yizhilong28.iteye.com/blog/1154098 在linux下跑一个多线程读取文件的程序,待读取文件有数十万个.程序读取过程中抛出如下异常*****(Too man ...

  10. [Ubuntu]明明白白安装中文字体

    Ubuntu 6.06 dapper对中文的支持应该说是一向不错的,包括现在默认安装就包含了scim输入法.然而,一般安装的ubuntu虽然能够显示.输入中文了,但总觉 得所显示的中文像歪瓜裂枣,很是 ...