将基元类型传递给WCF RESTful服务
在过去的几个小时里,我一直在碰壁,下面是我们要做的:一个方法需要一个基元/简单类型作为请求主体。最初我们尝试使用布尔值,但没有成功,所以我们尝试使用字符串和对象。同样的事情 这是服务器端代码将基元类型传递给WCF RESTful服务,wcf,json,rest,primitive-types,Wcf,Json,Rest,Primitive Types,在过去的几个小时里,我一直在碰壁,下面是我们要做的:一个方法需要一个基元/简单类型作为请求主体。最初我们尝试使用布尔值,但没有成功,所以我们尝试使用字符串和对象。同样的事情 这是服务器端代码 [OperationContract] [WebInvoke(UriTemplate = "/foo/{foo_id}/bar", Method = "POST", ResponseFormat=WebMessageFormat.JSON)] string G(string foo_id, string c
[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\u world”
封装在一个json对象中,比如{“content”:“hello\u world”},但没有成功
有什么想法吗?对我来说很好,以下是我的代码:
[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"
忘记提到行为:如果我在方法中放置断点,它不会被命中。相反,我只是得到一个400错误返回到Fiddler。我认为请求格式是根据内容/类型动态获取的?不,这是HTTP应该做的,WCF REST没有的众多功能之一。
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:54287
Content-Length: 7
"Hello"