如何使用URI参数调用WCF REST-Service

How to call WCF REST-Service with URI parameter

本文关键字:WCF REST-Service 调用 参数 何使用 URI      更新时间:2023-09-26

我有一个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

当我用一个正常的字符串调用服务时,一切正常

../liveEvents/qwertz

当我用编码的Uri调用它时(我直接在浏览器中输入请求),我得到一个"没有找到端点"的错误。

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

任何想法可能是什么问题,以及如何安全地传递一个URI作为参数与JavaScript的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)

你只需调用这个url:

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

希望有所帮助

就是不要使用uritemplate。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