【问题标题】:Not found 404 while accessing WCF Rest service (WebGet) when url segment is json string当 url 段为 json 字符串时,访问 WCF Rest 服务 (WebGet) 时未找到 404
【发布时间】:2016-10-20 05:21:23
【问题描述】:

在我的项目中,我必须将动态 Json 字符串传递给 WCF Rest 服务 Web 获取。

当我传递一些字符串时,一切正常。

http://localhost:58014/MyService.svc/api/queries/somestirng

但是当我将 json 字符串作为 url 段传递时,会发生错误。

{"Key":"UserHomePageInfoQuery","Message":"{\"UserName\":\"usr83\"}"}

http://localhost:58014/MyService.svc/api/queries/257b%2522Key%2522%253a%2522UserHomePageInfoQuery%2522%252c%2522Message%2522%253a%2522%257b%255c%2522UserName%255c%2522%253a%255c%2522usr83%255c%2522%257d%2522%257d

这是我的服务运营合同定义

    [OperationContract]
    [WebGet(UriTemplate = "api/queries/{queryString}",                        
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    Response Query(string queryString);

我知道我可以使用 POST,但所有服务请求都是查询

我有另一个 POST 命令服务

[OperationContract]
        [WebInvoke(UriTemplate = "api/commands/",
            Method = "POST",            
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Json)]       
        Response Execute(Request request)

【问题讨论】:

  • 它一定是GET吗?您可以简单地 POST JSON 字符串。
  • 是的,因为只有查询操作通过这个服务。我有另一个命令服务作为 post
  • 不是也可以把WebGet改成WebInvoke吗?还是要求,此方法应仅接受 GET?
  • 是的。它应该只接受 GET。为了清楚起见,我编辑问题
  • 你检查过这个吗? stackoverflow.com/questions/24551412/…

标签: c# json wcf wcf-rest


【解决方案1】:

我在 {queryString} 参数中使用了通配符 (*),表示“URI 的其余部分”

https://msdn.microsoft.com/en-us/library/bb675245.aspx

    [OperationContract]
    [WebGet(UriTemplate = "api/queries/{*queryString}",                        
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    Response Query(string queryString);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2014-10-06
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多