簡體   English   中英

如何獲取RESTful Web服務的請求URI

[英]How to get the request URI of a RESTful webservice

我有一個web服務,在幾個不同的網站下使用,並想知道請求通過哪些網站(並根據該數據返回不同的東西)。 我該怎么做呢?

我有一個網站example.com,在/webservice.svc中有一個web服務。 當客戶端通過http://client1.example.com/webservice.svc/hello訪問此站點時,我想說“你好,client1!” 當他們來到http://client2.example.com/webservice.svc/hello時,它應該說“你好,client2!”。

所以client1或client2取決於subhost(或應用程序目錄)

假設您正在使用WCF,您可以嘗試: System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.Headers["Referer"]

您的示例中的結果將是http://client1.example.com/webservice.svc/hellohttp://client2.example.com/webservice.svc/hello 然后,您可以解析該字符串是否存在client1或client2。

您可能想要檢查空值。

結束了使用HttpContext.Current.Request.Url.ToString();

它最初在我嘗試時返回“對象引用沒有設置為對象的實例”,但發現在我的類聲明之前以及在web.config中設置[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]這樣做並允許訪問我想要的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM