【问题标题】:How to call WCF REST-Service with URI parameter如何使用 URI 参数调用 WCF REST-Service
【发布时间】:2015-07-28 10:04:43
【问题描述】:

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

【问题讨论】:

    标签: javascript wcf rest uri


    【解决方案1】:

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

    Get(string id)
    

    你刚才用这个网址打电话:

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

    希望对你有帮助

    【讨论】:

    • 我在问题中添加了方法签名。使用 ?artistUri= 没有帮助。即使是带有 /?artistUri=qwertz 的测试请求现在也会失败,并出现“找不到端点”错误。
    • 因此,根据您的问题,您的 GetLiveEventsList 方法响应 Get 动词。您可以使用 yourdns/liveEvents/?artistUri=qwertz 调用在浏览器中打开的方法吗?有用吗?
    【解决方案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
    

    附加的,这里不需要编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 2020-01-24
      相关资源
      最近更新 更多