原創|使用教程|編輯:郝浩|2013-04-07 13:53:23.000|閱讀 868 次
概述:在DXTREME中我們提供了一個類庫用于幫助用戶從OData服務中檢索數據,執行CRUD操作: ODataContext 。作為一款移動瀏覽器的應用程序,一般是托管在不同的OData服務域中, ODataContext必須適用于OData服務,這個OData也必須滿足一些需求。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在DXTREME ENTERPRISE中我們提供了一個類庫用于幫助用戶從OData服務中檢索數據,執行CRUD操作: ODataContext 。
作為一款移動瀏覽器的應用程序,一般是托管在不同的OData服務域中, ODataContext必須適用于OData服務,這個OData也必須滿足一些需求,具體的如下:
1、OData服務必須支持的JSONP的數據格式。
2、OData服務必須支持跨域POST,MERGE和DELETE操作。
啟用JSONP
要啟用JSONP支持,需要使用JSONP中提供的解決方案,以及支持ADO.NET 數據服務條的url控制格式。
需要做的就是在“Download”中下載可用集,以及添加一個引用到OData服務對象的集上。然后簡單的應用JSONPSupportBehaviorAttribute屬性與服務類。
想要讓這個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";
啟用跨域的操作支持
為了讓客戶端發送跨域請求,處理HttpApplication.BeginRequest事件。這個事件處理中,檢查檢是否在HttpRequest的headers上包含一個Origin header。如果指定的起源,添加Access-Control-Allow-Origin 和 Access-Control-Allow-Credentials headers到響應上,此外復制Access-Control-Request-Method和Access-Control-Request-Headers請求的Headers值到Access-Control-Allow-Methods 以及Access-Control-Allow-Headers的響應headers中,如果HttpMethod是“OPTIONS”,設置HttpResponce。 StatusCode值設置為204。
具體實現的步驟如下:
1、如果沒有補充的話,添加Global.asax文件到Odata服務項目。
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事件處理程序上執行這個方法:
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
最后,打開網頁,配置文件,以及添加代碼到<configuration>部分中:
<system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都控件