简体   繁体   English

WCF服务RESTful

[英]Wcf service RESTful

I made my method with post like this: 我用这样的帖子制作了我的方法:

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

The UserEnteredName class has just one property - string. UserEnteredName类只有一个属性-字符串。

And it works. 而且有效。 But, I need to make it to be get, not post. 但是,我需要使它成为获取对象,而不是发布对象。

I tried with this: 我尝试了这个:

[WebInvoke(Method= "GET", UriTemplate = "GetHuman?username={John}", 
           ResponseFormat = WebMessageFormat.Json, 
           RequestFormat = WebMessageFormat.Json)]

But it doesn't work. 但这是行不通的。 What do I need to change? 我需要更改什么?

According to your UriTemplate , your method would have to look something like 根据您的UriTemplate ,您的方法必须看起来像

Human GetHuman(string John)

I suspect you are mistakenly putting a possible parameter value in your UriTemplate . 我怀疑您在UriTemplate中错误地输入了可能的参数值。 Try something like 尝试类似

[WebInvoke(Method= "GET", UriTemplate = "GetHuman?username={userName}", 
           ResponseFormat = WebMessageFormat.Json, 
           RequestFormat = WebMessageFormat.Json)]
Human GetHuman(string userName)

Also, for GET , you can use the WebGetAttribute , which is slightly cleaner. 另外,对于GET ,您可以使用WebGetAttribute ,它稍微干净一些。


I would change your method to take a string parameter and construct the UserEnteredName instance in the method body. 我将更改您的方法以采用string参数,并在方法主体中构造UserEnteredName实例。 It may be possible to use your UserEnteredName type as a parameter if it uses the TypeConverterAttribute , but I have never done this, so I can't say how easy (or not) it is. 如果使用TypeConverterAttribute ,则可以将UserEnteredName类型用作参数,但是我从未做过,所以我不能说它有多容易(或没有)。 See the WCF Web HTTP Programming Model Overview , specifically the UriTemplate Query String Parameters and URLs section. 请参阅WCF Web HTTP编程模型概述 ,尤其是UriTemplate查询字符串参数和URL部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM