如何调用带有多个参数的 RESTful WCF 服务方法?
我有一个 RESTful WCF 服务,其方法声明如下: 这
[OperationContract(Name = "IncrementAge")]
[WebInvoke(UriTemplate = "/", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
Person IncrementAge(Person p);
是实现:
public Person IncrementAge(Person p)
{
p.age++;
return p;
}
因此,它采用 Person 复杂类型,将age 属性增加 1,然后使用 JSON 序列化将其返回。我可以通过向服务发送 POST 消息来测试它,如下所示:
POST http://localhost:3602/RestService.svc/ HTTP/1.1
Host: localhost:3602
User-Agent: Fiddler
Content-Type: application/json
Content-Length: 51
{"age":25,"firstName":"Hejhaj","surName":"Csuhaj"}
这有效。如果我想要这样的方法怎么办?
Person IncrementAge(Person p, int amount);
所以它有多个参数。我应该如何构造 POST 消息才能使其工作?这可能吗?
谢谢
I have a RESTful WCF service with a method declared like this:
[OperationContract(Name = "IncrementAge")]
[WebInvoke(UriTemplate = "/", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
Person IncrementAge(Person p);
Here's the implementation:
public Person IncrementAge(Person p)
{
p.age++;
return p;
}
So it takes the Person complex type, increments the age property by one, and spits it back, using JSON serialization. I can test the thing by sending a POST message to the service like this:
POST http://localhost:3602/RestService.svc/ HTTP/1.1
Host: localhost:3602
User-Agent: Fiddler
Content-Type: application/json
Content-Length: 51
{"age":25,"firstName":"Hejhaj","surName":"Csuhaj"}
This works. What if I'd like to have a method like this?
Person IncrementAge(Person p, int amount);
So it'd have multiple parameters. How should I construct the POST message for this to work? Is this possible?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将消息正文样式设置为包裹,以便您可以在 POST 请求正文中接受多个参数。
您的方法签名将是:
请求的正文将如下所示:
外部 JSON 对象是匿名包装器。
You should make the message body style wrapped so that you can accept multiple arguments in the POST request body.
Your method signature will be:
And the body of the request will look like:
The outer JSON object is the anonymous wrapper.
您可以使用查询字符串参数,
我认为 WCF 签名将是:
You could use a query string parameter,
I think the WCF signature would be: