如何使用 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
当我用普通字符串调用服务时,一切正常
../liveEvents/qwertz
当我使用编码后的 Uri 调用它时(我直接在浏览器中输入请求),我收到 "no endpoint found" 错误。
../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);
将 url 作为查询字符串参数传递。
假设您的 Get 方法签名是这样的:
Get(string id)
你刚刚用这个 url 调用:
'/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
补充,此处无需编码
我有一个 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 调用它时(我直接在浏览器中输入请求),我收到 "no endpoint found" 错误。
../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);
将 url 作为查询字符串参数传递。 假设您的 Get 方法签名是这样的:
Get(string id)
你刚刚用这个 url 调用:
'/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
补充,此处无需编码