[轉]ASP.NET Web API對OData的支援

來源:互聯網
上載者:User

標籤:世界   mil   dash   邏輯   org   conf   儲存   ros   news   

http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html

在SOA的世界中,最重要的一個概念就是契約(contract)。在雲端運算的世界中,有關通訊的最重要的概念也是契約。XML具有強大對資料的描述能力,Atom格式和AtomPub都建立在XML之上,在Google和微軟的推動下,也已經成為標準。但是,Atom/AtomPub和ODBC/OLEDB這樣的真正資料互動協議相比較,還有著根本上的欠缺:缺乏資料類型的具體描述,降低了互動效能。缺乏對資料查詢的控制能力,比如返回特定的資料集合的區間,或者說分頁能力等等。微軟基於EDM模型釋出了:OData,這裡也可以看出Entity Framework對於NHibernate這樣的ORM的工具不同的戰略考慮。

在.NET中,早期是用Remoting/Webservice來處理所有程式間的通訊,從.NET 3.0開始使用WCF統一了通訊模型,ASP.NET MVC4的推出,形成大的One ASP.NET戰略,增加了WebAPI和SingalR作為通訊服務:

開放資料協議(OData)是一個查詢和更新資料的Web協議。OData應用了web技術如HTTP、Atom發布協議(AtomPub)和JSON等來提供對不同應用程式,服務和儲存的資訊訪問。除了提供一些基本的操作(像增刪改查),也提供了一些進階的操作類似過濾資料和實體的導航。OData擴充了上述的協議但是不是取代他們。他可以被XML(ATOM)或者JSON取代但是OData的重要在於它符合REST原則。在某種意義上,它建立在‘簡單‘的REST HTTP 服務上,並且有著清晰的目標——簡化和標準化我們操作和查詢資料的方式。如果你過去在給你的REST服務建立搜尋、過濾、或者分頁API的時候感覺很麻煩,那麼OData將是一個不錯的選擇。

目前很多介面,無論是基於SOAP、REST還是別的都在交換資料時使用不同的模式。這種方法隨後返回一大堆客戶記錄。你隨後可以決定添加分頁支援。你希望將結果捆綁在一個網格中,並對資料排序。最後,決定想要查詢的東西,通過比如郵遞區號來查詢。

  首先是,沒有建立泛型用戶端的途徑,而這些和API緊密聯絡,因為它不知道參數的順序或者模式被使用的順序。因為不能建立泛型用戶端,你必須為每一個你希望暴露的API建立用戶端。簡單的基礎HTTP API可以實現,但其仍舊很昂貴。逐漸增多的多樣性用戶端與這些API通訊加劇了這個問題。

  這種模式的第二個問題是它迫使開發人員進行很艱難的權衡。我應該暴露多少個查詢?你必要在暴露每一個你能想到內容和少暴露一些,從而削弱服務之間協調。前者導致API 需要管理的介面的增加,後者會導致我們通常所說的“資料豎井”,也就是關鍵資料在特定模式中鎖定,其他應用不能夠簡單應用,因為它沒有以一種需要的方式暴露給這個應用。服務試圖比單一應用要獲得更長久一些,因此你需要以一種方式設計API,使其能夠持久,所以如果你發現你需要添加服務借口的新版本可不太好辦,比如建立新的用戶端。在很多案例中,服務開發人員和用戶端開發人員並不是同一個人,因而改變服務介面簡直就是不可能的事情。

  通過OData,我們採取不同的方法。取代建立用戶端簽名和參數,我們問了如下的問題:“如果你將資料集作為源處理,並為最頻繁使用的操作定義模式,像查詢、分頁、排序、建立、刪除和更新,服務介面因該是什麼樣子的?” 這也就導致OData的建立。OData解決了上面提到的關鍵服務設計挑戰。

我們來看一下啟用OData協議的WebAPI的例子:

http://localhost:8080/api/meetings

http://localhost:8080/api/meetings?$filter=(Leader eq ‘Mark Nichols’)

http://localhost:8080/api/meetings?$top=2

http://localhost:8080/api/meetings?$filter=MeetingDate eq datetime’2013-01-17′

在項目中啟用OData查詢,首先在項目加入Web API的OData支援,通過Nuget 尋找ASP.NET Web API OData

Microsoft.AspNet.WebApi.OData提供可一系列的類擴充了Web API。

在項目中啟用OData查詢:

public static void Register(HttpConfiguration config) {     // ...     config.EnableQuerySupport();     // ... }

如果是使用self-hosting方式,在HttpSelfHostConfiguration上啟用EnableQuerySupport():

var config = new HttpSelfHostConfiguration(new Uri("http://localhost:8080")); config.EnableQuerySupport();

然後將Controls上的Action的返回結果更改為IQueryable,並打上標籤[Queryable()]:

        // GET api/Meeting        [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]        public IQueryable<Meeting> Get()        {            return _scheduledMeetings.AsQueryable();        }需要添加 using System.Web.Http.OData.Query; 我們下AllowedQueryOptions 看支援那些OData的類型:
   // Summary:    //     OData query options to allow for querying.    [Flags]    public enum AllowedQueryOptions    {        // Summary:        //     A value that corresponds to allowing no query options.        None = 0,        //        // Summary:        //     A value that corresponds to allowing the $filter query option.        Filter = 1,        //        // Summary:        //     A value that corresponds to allowing the $expand query option.        Expand = 2,        //        // Summary:        //     A value that corresponds to allowing the $select query option.        Select = 4,        //        // Summary:        //     A value that corresponds to allowing the $orderby query option.        OrderBy = 8,        //        // Summary:        //     A value that corresponds to allowing the $top query option.        Top = 16,        //        // Summary:        //     A value that corresponds to allowing the $skip query option.        Skip = 32,        //        // Summary:        //     A value that corresponds to allowing the $inlinecount query option.        InlineCount = 64,        //        // Summary:        //     A value that corresponds to the default query options supported by System.Web.Http.QueryableAttribute.        Supported = 121,        //        // Summary:        //     A value that corresponds to allowing the $format query option.        Format = 128,        //        // Summary:        //     A value that corresponds to allowing the $skiptoken query option.        SkipToken = 256,        //        // Summary:        //     A value that corresponds to allowing all query options.        All = 511,    }

上面有個Format,我們可以進行格式化輸出。使用下面的代碼對Format進行資料格式化:

public static class WebApiConfig    {        public static void Register(HttpConfiguration config)        {            config.Routes.MapHttpRoute(                name: "DefaultApi",                routeTemplate: "api/{controller}/{id}",                defaults: new { id = RouteParameter.Optional }            );           

config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json"); config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");

            config.EnableQuerySupport();        }

另外需要注意的一點是OData查詢是大小寫敏感的。

我將使用Fiddler去測試這個服務

我們沒有寫任何一個特別的邏輯去支援這些功能,全部都由架構來提供的。是不是OData為你的搜尋、過濾、或者分頁API的時候提供了一個很好的選項。

然而,如果要向組織外部公開可查詢的操作,可以利用查詢驗證添加一個保護層以保護我們的服務。微軟的程式經理Hongmei Ge介紹了幾種在Queryable API中添加驗證的情境。

Hongmei指出的第一個情境是,使用AllowedQueryOptions屬性,只允許包含$top和$skip的查詢。如下所示:

[Queryable(AllowedQueryOptions = AllowedQueryOptions.Skip | AllowedQueryOptions.Top)] public IQueryable Get(int projectId)

還可以使用MaxTop和MaxSkip屬性將$top和$skip的最大值限制在100和200:

[Queryable(MaxTop = 100)] public IQueryable Get(int projectId)

[Queryable(MaxSkip = 200)] public IQueryable Get(int projectId)

利用AllowedOrderByProperties,可以將結果按Id屬性排序,因為按其他屬性排序可能會很慢:

[Queryable(AllowedOrderByProperties = "Id")] public IQueryable Get(int projectId)

如果允許用戶端在$filter內使用相等比較,應該使用AllowedLogicalOperators對其進行驗證:

[Queryable(AllowedLogicalOperators = AllowedLogicalOperators.Equal)] public IQueryable Get(int projectId)

將AllowedArithmeticOperators設定為None,就可以關閉$filter中的算術操作:

[Queryable(AllowedArithmeticOperators = AllowedArithmeticOperators.None)] public IQueryable Get(int projectId)

你還可以使用AllowedFunctions屬性來限制$filter中的函數使用:

[Queryable(AllowedFunctions = AllowedFunctions.StartsWith)] public IQueryable Get(int projectId)

上面的代碼意味著只能在$filter中使用StartsWith函數。

Hongmei還示範了進階情境中的查詢驗證,如為$skip、$top、$orderby、$filter自訂預設驗證邏輯,以及使用ODataQueryOptions來驗證查詢。

相關文章:

OData Developers Reference: http://www.odata.org/developers/

OData in ASP.NET: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api

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

OData Security: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-security-guidance

Add an OData Feed to Your App Using Web API:http://marknic.net/2013/03/02/add-an-odata-feed-to-your-app-using-web-api/

Working with OData Queries in ASP.NET Web API:http://www.codeguru.com/csharp/.net/working-with-odata-queries-in-asp.net-web-api.htm 

在ASP.NET Web API OData中利用Queryable API進行驗證: http://www.infoq.com/cn/news/2013/02/queryable-api

一個建立 OData 的新選項: Web API:http://msdn.microsoft.com/zh-cn/magazine/dn201742.aspx 

Building OData Service using ASP.Net Web API Tutorial – Part 1

範例程式碼下載: http://files.cnblogs.com/shanyou/WebApiOData.zip

 

[轉]ASP.NET Web API對OData的支援

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.