代码之家  ›  专栏  ›  技术社区  ›  Oli

将基元类型传递给WCF RESTful服务

  •  1
  • Oli  · 技术社区  · 15 年前

    在过去的几个小时里,我一直在碰壁,下面是我们要做的:一个方法需要一个基元/简单类型作为请求主体。最初我们尝试使用布尔值,但没有成功,所以我们尝试使用字符串和对象。同样的事情。

    这是服务器端代码

    [OperationContract]
    [WebInvoke(UriTemplate = "/foo/{foo_id}/bar", Method = "POST", ResponseFormat=WebMessageFormat.JSON)]
    string G(string foo_id, string content);
    

    以下是《小提琴手》中的要求:

    User-Agent: Fiddler
    Host: localhost
    Content-Type: 'application/json',
    Content-Length: 19
    

    正文:

    "hello_world"
    

    我们试着包起来 "hello_world" 在json对象中,如{“content”:“hello_world},但没有运气。

    有什么想法吗?

    1 回复  |  直到 15 年前
        1
  •  2
  •   Dave Clemmer manu    12 年前

    对我来说很好,以下是我的代码:

    [OperationContract]
    [WebInvoke(UriTemplate = "/foo/{foo_id}/bar", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string G(string foo_id, string content)
    {
        return content + foo_id;
    }
    

    您没有设置请求格式(我知道这很痛苦:)

    这是我的小提琴手请求:

    User-Agent: Fiddler
    Content-Type: application/json
    Host: localhost:54287
    Content-Length: 7
    "Hello"