Wcf服务RESTful

Wcf服务RESTful,wcf,json,post,get,Wcf,Json,Post,Get,我用post制定的方法如下: [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] List<Human> GetHuman(UserEnter

我用post制定的方法如下:

[OperationContract]
[WebInvoke(Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Bare)]
List<Human> GetHuman(UserEnteredName humanName);

但它不起作用。我需要更改什么?

根据您的
UriTemplate
,您的方法必须如下所示

Human GetHuman(string John)
[WebInvoke(Method= "GET", UriTemplate = "GetHuman?username={userName}", 
           ResponseFormat = WebMessageFormat.Json, 
           RequestFormat = WebMessageFormat.Json)]
Human GetHuman(string userName)
我怀疑您在
UriTemplate
中错误地输入了一个可能的参数值。试试像这样的东西

Human GetHuman(string John)
[WebInvoke(Method= "GET", UriTemplate = "GetHuman?username={userName}", 
           ResponseFormat = WebMessageFormat.Json, 
           RequestFormat = WebMessageFormat.Json)]
Human GetHuman(string userName)
另外,对于
GET
,您可以使用稍微干净一点的



我将更改您的方法,以获取
字符串
参数,并在方法体中构造
UserEnteredName
实例。如果您的
UserEnteredName
类型使用了
TypeConverterAttribute
,则可以使用它作为参数,但我从来没有这样做过,所以我不能说这有多容易(或不容易)。请参阅,特别是UriTemplate查询字符串参数和URL部分。

发布错误。。“不工作”是什么意思?@andreapier错误是500内部服务器错误。也许您试图从非基本域获取一些json?ie:您的基本域是www.mydomain.com,您正在从inner.mydomain.com?@andreapier发出ajax调用。我尝试使用UriTemplate=“Service1.svc/json/GetHuman?custName=John”,但仍然没有结果。我只想知道您是否试图违反同源策略。在这种情况下,您需要使用jsonP,而不是经典的json。让我知道。顺便说一句,adrift是正确的,在我尝试之前尝试一下,但它不起作用。我的方法如下所示:List GetHuman(UserEnteredName humanName);但是UserEnteredName是一个包含字符串属性UserName的类,我很惊讶你甚至可以编译它。。。如何传递字符串而不是UserEnteredName?也许你为我们写了一个简单的例子?或者,您也可以尝试使用一个以字符串作为输入的GetHuman方法的包装器parameter@andreapier我更新了我的问题-关于UserEnteredName类。