JSON的WCF REST POST:参数为空 [英] WCF REST POST of JSON: Parameter is empty
问题描述
使用Fiddler将JSON消息发布到WCF服务.该服务使用System.ServiceModel.Activation.WebServiceHostFactory
Using Fiddler I post a JSON message to my WCF service. The service uses System.ServiceModel.Activation.WebServiceHostFactory
[OperationContract]
[WebInvoke
(UriTemplate = "/authenticate",
Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest
)]
String Authorise(String usernamePasswordJson);
进行POST时,我可以破解代码,但是参数 usernamePasswordJson 为空.为什么会这样?
When the POST is made, I am able to break into the code, but the parameter usernamePasswordJson is null. Why is this?
注意:刚好当我将 BodyStyle 设置为 Bare 时,该帖子甚至没有得到我要调试的代码.
Note: Strangly when I set the BodyStyle to Bare, the post doesn't even get to the code for me to debug.
这是提琴手屏幕:
推荐答案
您将参数声明为String类型,因此它需要一个JSON字符串-并且您正在向其传递JSON对象.
You declared your parameter as type String, so it is expecting a JSON string - and you're passing a JSON object to it.
要接收该请求,您需要拥有与以下合同相似的合同:
To receive that request, you need to have a contract similar to the one below:
[ServiceContract]
public interface IMyInterface
{
[OperationContract]
[WebInvoke(UriTemplate = "/authenticate",
Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
String Authorise(UserNamePassword usernamePassword);
}
[DataContract]
public class UserNamePassword
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Password { get; set; }
}
这篇关于JSON的WCF REST POST:参数为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!