一、概述
时间也算充足,抽点时间总结下OData的常用的使用方式,开放数据协议(OData)是一个查询和更新数据的Web协议。OData应用了web技术如HTTP、Atom发布协议(AtomPub)和JSON等来提供对不同应用程序,服务和存储的信息访问。除了提供一些基本的操作(像增删改查),也提供了一些高级的操作类似过滤数据和实体的导航。OData扩展了上述的协议但是不是取代他们。他可以被XML(ATOM)或者JSON取代但是OData的重要在于它符合REST原则。在某种意义上,它建立在'简单'的REST HTTP 服务上,并且有着清晰的目标——简化和标准化我们操作和查询数据的方式。如果你过去在给你的REST服务创建搜索、过滤、或者分页API的时候感觉很麻烦,那么OData将是一个不错的选择。
二、WEB API使用:
1、通过NuGet获取 Microsoft.AspNet.WebApi.OData 第三方插件DLL,Microsoft.AspNet.WebApi.OData提供可一系列的类扩展了Web API。
2、WebApiConfig进行下面代码添加,开启OData支持;
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using System.Net.Http.Formatting; using System.Web.Http.OData.Extensions; namespace MyMVCOData { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); //启用Odata查询 config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json"); config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml"); //config.EnableQuerySupport(); config.AddODataQueryFilter(); //config.SetTimeZoneInfo(TimeZoneInfo.Local); } } }
备注:
添加命名空间:using System.Net.Http.Formatting;
config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
功能说明:这段代码支持WebAPI数据接口显示可以以XML显示也可以以json格式显示,调用的时候如下:
http://localhost:port/api/ProjectManagent?$format=json 此处以json格式显示结果
http://localhost:port/api/ProjectManagent?$format=xml 此处以xml格式显示结果
3、编写接口 API Control
添加Control 代码如下:
using MyMVCOData.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.OData; using System.Web.Http.OData.Query; namespace MyMVCOData.Controllers { public class MeetingsController : ApiController { private readonly IList<Meeting> _scheduledMeetings; public MeetingsController() { _scheduledMeetings = new List<Meeting> { new Meeting { Id = "1", Leader = "Mark Nichols", MeetingDate = new DateTime(2013, 1, 17), Title = "Project X Planning" }, new Meeting { Id = "3", Leader = "Jim Phelps", MeetingDate = new DateTime(2013, 2, 8), Title = "Mission Discussion" }, new Meeting { Id = "6", Leader = "Barney Collier", MeetingDate = new DateTime(2013, 3, 12), Title = "Advanced Device Technology" }, new Meeting { Id = "7", Leader = "Willie Armitage", MeetingDate = new DateTime(2013, 5, 28), Title = "Maintaining a Strong Presence" }, new Meeting { Id = "9", Leader = "Cinnamon Carter", MeetingDate = new DateTime(2013, 2, 15), Title = "Company Fashion" }, new Meeting { Id = "10", Leader = "Rollin Hand", MeetingDate = new DateTime(2013, 1, 21), Title = "Magic Selling" } }; } // GET api/Meeting //[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)] //[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)] //[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Top | AllowedQueryOptions.Skip)] //[EnableQuery(MaxTop = 100)] 配置调用限制项目 [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All,MaxTop =100, MaxSkip = 200,PageSize =2,AllowedFunctions = AllowedFunctions.All)] public IQueryable<Meeting> Get() { return _scheduledMeetings.AsQueryable(); } } }
4、接口调用说明:
下表列举了一些常用的Odata操作:
操作 |
URL |
说明 |
$filter | http://localhost:8090/api/Meetings?$filter=ProductName eq 'Tofu' | 根据表达式的状态返回结果(返回ProductName 等于Tofu的Products) |
$orderby | http://localhost:8090/api/Meetings?$orderby=ProductName | 根据结果排序(根据ProductName列排序) |
$skip | http://localhost:8090/api/Meetings?$skip=10 | 越过结果中的n条数据,常用于分页 |
$top | http://localhost:8090/api/Meetings?$top=10 | 返回结果中的前n条记录,常用于分页 |
$select | http://localhost:8090/api/Meetings?$filter=ProductName eq 'Tofu'&$select=ProductName,UnitPrice | 选择需要返回的属性 |
$expand | http://localhost:8090/api/Meetings?$expand=Supplier | 返回Products中包含的导航属性(关联属性)Supplier |
$inlinecount | http://localhost:8090/api/Meetings?$inlinecount=allpages | 向服务器获取符合条件的资源总数(分页的total值) |
通过上面表格的内容,我们还可以通过组合查询条件来实现复杂的查询。
常用查询举例:
示例1:列出所有Product
URL:http://localhost:8914/Products
示例2,查询Products,只列出Name,Price例
URL:http://localhost:8914/Products?$select=Name,Price
示例3:列出Products(只有列Name,Price),包括Supplier
URL:http://localhost:8914/Products?$select=Name,Price&$expand=Supplier
示例4:过滤Products,只显示分类为Test的数据
URL:http://localhost:8914/Products?$filter=Category eq ’Test‘
示例5:过滤Products,只显示分类为Test的数据,并排序
URL:http://localhost:8914/Products?$filter=Category eq ’Test‘&$orderby=Price desc
$filter的其它的使用方式:
1. http://localhost/Products?$filter=Category eq 'Test'
过滤Category=Test
2.http://localhost/Products?$filter=Price lt 10
过滤Price小于10
3.http://localhost/Products?$filter=Price ge 5 and Price le 15
过滤5<=Price>=15
4.还可以使用数据库函数如:
$filter=substringof('zz',Name)
$filter=year(ReleaseDate) gt 2005
5.关于排序:
$orderby=Price
$orderby=Price desc
$orderby=Category,Price desc
6.还有一些过滤器如:
$skip,$top,$inlinecount等等
四、参考博客:
http://www.cnblogs.com/liuju150/p/4602715.html
http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html
五、后续问题:
针对查询结果时间的输出,目前输出的格式是:
"MeetingDate": "2016-08-27T08:27:46.2664375+08:00"
如将日期的输出格式是我们自定义的如:2016-08-27 08:27:46