如何使用URI参数

时间:2015-07-28 10:04:43

标签: javascript wcf rest uri

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

知道可能存在什么问题以及如何使用JavaScript将URI作为参数安全地传递给WCF REST服务?

提前致谢,
弗兰克

编辑:这是端点方法:

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

2 个答案:

答案 0 :(得分:0)

将url作为查询字符串参数传递。 假设您的Get方法签名是这样的:

Get(string id)

你刚用这个网址打电话:

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

希望有所帮助

答案 1 :(得分:0)

只是不要使用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

另外,这里不需要编码。

相关问题