原創(chuàng)|使用教程|編輯:郝浩|2013-04-07 13:53:23.000|閱讀 868 次
概述:在DXTREME中我們提供了一個(gè)類(lèi)庫(kù)用于幫助用戶(hù)從OData服務(wù)中檢索數(shù)據(jù),執(zhí)行CRUD操作: ODataContext 。作為一款移動(dòng)瀏覽器的應(yīng)用程序,一般是托管在不同的OData服務(wù)域中, ODataContext必須適用于OData服務(wù),這個(gè)OData也必須滿(mǎn)足一些需求。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
在DXTREME ENTERPRISE中我們提供了一個(gè)類(lèi)庫(kù)用于幫助用戶(hù)從OData服務(wù)中檢索數(shù)據(jù),執(zhí)行CRUD操作: ODataContext 。
作為一款移動(dòng)瀏覽器的應(yīng)用程序,一般是托管在不同的OData服務(wù)域中, ODataContext必須適用于OData服務(wù),這個(gè)OData也必須滿(mǎn)足一些需求,具體的如下:
1、OData服務(wù)必須支持的JSONP的數(shù)據(jù)格式。
2、OData服務(wù)必須支持跨域POST,MERGE和DELETE操作。
啟用JSONP
要啟用JSONP支持,需要使用JSONP中提供的解決方案,以及支持ADO.NET 數(shù)據(jù)服務(wù)條的url控制格式。
需要做的就是在“Download”中下載可用集,以及添加一個(gè)引用到OData服務(wù)對(duì)象的集上。然后簡(jiǎn)單的應(yīng)用JSONPSupportBehaviorAttribute屬性與服務(wù)類(lèi)。
想要讓這個(gè)JSONPSupportBehavior模塊為WCF Data Services 5.0工作,需要稍微修改其源代碼,你需要做的就是改變JSONPSupportInspector.AfterReceiveRequest method. Replace這一行。
httpmsg.Headers["Accept"] = "application/json, text/plain;q=0.5"
httpmsg.Headers["Accept"] = "application/json, text/plain;q=0.5,application/json;odata=verbose";
啟用跨域的操作支持
為了讓客戶(hù)端發(fā)送跨域請(qǐng)求,處理HttpApplication.BeginRequest事件。這個(gè)事件處理中,檢查檢是否在HttpRequest的headers上包含一個(gè)Origin header。如果指定的起源,添加Access-Control-Allow-Origin 和 Access-Control-Allow-Credentials headers到響應(yīng)上,此外復(fù)制Access-Control-Request-Method和Access-Control-Request-Headers請(qǐng)求的Headers值到Access-Control-Allow-Methods 以及Access-Control-Allow-Headers的響應(yīng)headers中,如果HttpMethod是“OPTIONS”,設(shè)置HttpResponce。 StatusCode值設(shè)置為204。
具體實(shí)現(xiàn)的步驟如下:
1、如果沒(méi)有補(bǔ)充的話,添加Global.asax文件到Odata服務(wù)項(xiàng)目。
2、將以下的方法添加到Global.asax文件中。
/// <summary> /// Enables cross domain POST, MERGE, DELETE for Firefox and Chrome /// This requires: /// <system.ServiceModel> /// <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> /// </summary> static void EnableCrossDomain() { string origin = HttpContext.Current.Request.Headers["Origin"]; if (string.IsNullOrEmpty(origin)) return; HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin); string method = HttpContext.Current.Request.Headers["Access-Control-Request-Method"]; if (!string.IsNullOrEmpty(method)) HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", method); string headers = HttpContext.Current.Request.Headers["Access-Control-Request-Headers"]; if (!string.IsNullOrEmpty(headers)) HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", headers); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.StatusCode = 204; HttpContext.Current.Response.End(); } } [VB.NET]Open in popup window ''' <summary> ''' Enables cross domain POST, MERGE, DELETE for Firefox and Chrome ''' This requires: ''' <system.ServiceModel> ''' <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> ''' </summary> Shared Sub EnableCrossDomain() Dim origin As String = HttpContext.Current.Request.Headers("Origin") If String.IsNullOrEmpty(origin) Then Return End If HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin) Dim method As String = HttpContext.Current.Request.Headers("Access-Control-Request-Method") If (Not String.IsNullOrEmpty(method)) Then HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", method) End If Dim headers As String = HttpContext.Current.Request.Headers("Access-Control-Request-Headers") If (Not String.IsNullOrEmpty(headers)) Then HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", headers) End If HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true") If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then HttpContext.Current.Response.StatusCode = 204 HttpContext.Current.Response.End() End If End Sub
在Application_BeginRequest事件處理程序上執(zhí)行這個(gè)方法:
protected void Application_BeginRequest(object sender, EventArgs e) { EnableCrossDomain(); } [VB.NET]Open in popup window Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) EnableCrossDomain() End Sub
最后,打開(kāi)網(wǎng)頁(yè),配置文件,以及添加代碼到<configuration>部分中:
<system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel>
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件