簡體   English   中英

如何使用URI參數調用WCF REST服務

[英]How to call WCF REST-Service with URI parameter

我有一個WCF Rest-Service,它的方法需要字符串參數。 此參數是Uri。 為了能夠將URI用作REST服務的參數,我使用了JavaScript方法encodeURIComponent來對URI進行編碼

http://creativeartefact.org/example/fa9eb3e7-8297-4541-81ec-e9e9be6e6638

變成

http%3A%2F%2Fcreativeartefact.org%2Fexample%2Ffa9eb3e7-8297-4541-81ec-e9e9be6e6638

當我使用普通的String調用服務時,一切都很好

../liveEvents/qwertz

當我使用編碼的Uri調用它時(我在瀏覽器中直接輸入了請求),出現“找不到端點”錯誤。

../liveEvents/http%3A%2F%2Fcreativeartefact.org%2Fexample%2Ffa9eb3e7-8297-4541-81ec-e9e9be6e6638

任何想法都有可能是什么問題,以及如何使用URI安全地將URI作為參數傳遞給WCF REST服務?

提前致謝,
坦率

編輯:這是端點方法:

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "liveEvents/{artistUri}")]
IList<LiveEvent> GetLiveEventsList(string artistUri);

將url作為查詢字符串參數傳遞。 假設您的Get方法簽名如下:

Get(string id)

您只需使用以下網址進行調用:

'/liveEvents/?id=' + encodeURIComponent('http://test.com/?name=Foo&id=2')

希望能幫助到你

只是不要使用UriTemplates。 WCF也可以在沒有它們的情況下工作,從JavaScript調用時,它們也沒有任何區別(據我所知)。

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
IList<LiveEvent> GetLiveEventsList(string artistUri);

像這樣致電服務:

http://theserver.com/Service.svc/GetLiveEventsList?artistUri=http://creativeartefact.org/example/fa9eb3e7-8297-4541-81ec-e9e9be6e6638

另外,這里不需要編碼。

暫無
暫無

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

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