一、概述
時間也算充足,抽點時間總結下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