Wcf服务RESTful [英] Wcf service RESTful
问题描述
[OperationContract]
[WebInvoke(Method =POST ,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
odyStyle = WebMessageBodyStyle.Bare)]
List< Human> GetHuman(UserEnteredName,人名);
UserEnteredName类只有一个属性 - 字符串。
它有效。但是,我需要使它得到,而不是后。
我试过这个:
[WebInvoke(Method =GET,UriTemplate =GetHuman?username = {John},
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
但它不起作用。根据你的 UriTemplate
,我需要改变什么?
人类GetHuman(字符串约翰)
我怀疑你错误地将一个可能的参数值放在 UriTemplate
中。尝试像
[WebInvoke(Method =GET,UriTemplate =GetHuman?username = {userName},
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
人类GetHuman(字符串用户名)
另外,对于 GET
,您可以使用 WebGetAttribute
,它稍微干净一点。
我会改变你的方法来取一个 string
参数并构造 UserEnteredName方法体中的
实例。如果它使用 TypeConverterAttribute
,可能可以使用 UserEnteredName
类型作为参数,但我从来没有这样做过,所以我不能说它有多容易(或不)。请参阅 WCF Web HTTP编程模型概述,特别是 UriTemplate查询字符串参数和网址部分。
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.
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
Human GetHuman(string John)
I suspect you are mistakenly putting a possible parameter value in your 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.
I would change your method to take a string
parameter and construct the UserEnteredName
instance in the method body. 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. See the WCF Web HTTP Programming Model Overview, specifically the UriTemplate Query String Parameters and URLs section.
这篇关于Wcf服务RESTful的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!