问题描述
我正在尝试调用WCF休息服务,如下所示:
I'm trying to invoke a WCF rest service as shown below:
[WebInvoke(UriTemplate = "Login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string Process(string AuthenticationInfo)
{
我正在尝试使用以下提琴手2中的以下内容调用它:
I'm trying to invoke it using the following below in Fiddler 2:
User-Agent: Fiddler
Host: localhost
content-type: application/json;charset=utf-8
content-length: 0
data: {"AuthenticationInfo": "data"}
我在方法中有一个断点,它确实碰到了断点,但是AuthenticationInfo的值始终为null,而不是"data".
I have a breakpoint in the method, and it does hit the breakpoint, but the value for AuthenticationInfo is always null, and not "data".
我在做什么错了?
谢谢.
推荐答案
[WebInvoke]
属性的默认正文样式"为裸",这意味着输入(对于您的情况为"data"
)必须为发送原样".您要发送的是输入的包装的版本(即包装在键为参数名称的对象中.
The default "body style" of the [WebInvoke]
attribute is "Bare", which means that the input (in your case, "data"
) must be sent "as is". What you're sending is a wrapped version of the input (i.e., wrapped in an object whose key is the parameter name.
可以通过两种方法进行此工作:更改WebInvoke
声明以包含BodyStyle参数:
There are two ways you can make this work: either change the WebInvoke
declaration to include the BodyStyle parameter:
[WebInvoke(
UriTemplate = "Login",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string Process(string AuthenticationInfo)
或者您可以更改请求以发送参数"bare":
Or you can change the request to send the parameter "bare":
POST .../Login HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Type: application/json;charset=utf-8
Content-Length: 6
"data"
这篇关于无法使用Fiddler将JSON发布数据传递到WCF REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!