如何调用带有多个参数的 RESTful WCF 服务方法?

发布于 2024-10-10 00:49:29 字数 809 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-10-17 00:49:29

您应该将消息正文样式设置为包裹,以便您可以在 POST 请求正文中接受多个参数。

您的方法签名将是:

[OperationContract(Name = "IncrementAge")]
[WebInvoke(UriTemplate = "/", Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Person IncrementAge(Person p, int amount);

请求的正文将如下所示:

{"p": {"age":25,"firstName":"Hejhaj","surName":"Csuhaj"}, "amount": 1}

外部 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:

[OperationContract(Name = "IncrementAge")]
[WebInvoke(UriTemplate = "/", Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Person IncrementAge(Person p, int amount);

And the body of the request will look like:

{"p": {"age":25,"firstName":"Hejhaj","surName":"Csuhaj"}, "amount": 1}

The outer JSON object is the anonymous wrapper.

旧人哭 2024-10-17 00:49:29

您可以使用查询字符串参数,

POST /RestService.svc/Incrementor?amount=23
{...}

我认为 WCF 签名将是:

[OperationContract(Name = "IncrementAge")]
[WebInvoke(UriTemplate = "/?amount={amount}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
Person IncrementAge(int amount, Person p);

You could use a query string parameter,

POST /RestService.svc/Incrementor?amount=23
{...}

I think the WCF signature would be:

[OperationContract(Name = "IncrementAge")]
[WebInvoke(UriTemplate = "/?amount={amount}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
Person IncrementAge(int amount, Person p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文