开发者

How to post a List<T> to a WCF Rest service

I have a class

[DataContract]
public class Test
{
[DataMemeber]
public string A {get;set;}
[DataMemeber]
public string B {get;开发者_StackOverflow社区set;}
[DataMemeber]
public string C {get;set;}
}

I have a Restful WCF method

[WebInvoke(UriTemplate = "checkupdates",
ResponseFormat = WebMessageFormat.Json, 
BodyStyle=WebMessageBodyStyle.WrappedRequest)]

List<Test> CheckForUpdates(List<Test> testing);

How can I post the List object to the service? This is from a wpf client.

Thanks


List is equivalent to an array, so the value should be represented as a JSON array. And since the body style says that the request needs to be wrapped, then you should wrap the JSON array in an object with a field named as the parameter:

{"testing":[
    {"A":"Value of A1","B":"Value of B1","C":"Value of C1"},
    {"A":"Value of A2","B":"Value of B2","C":"Value of C2"},
    {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}]}

If the request were not wrapped (BodyStyle of Bare or WrappedResponse) then you wouldn't need the wrapping object, and this would be a request for the operation:

[
  {"A":"Value of A1","B":"Value of B1","C":"Value of C1"},
  {"A":"Value of A2","B":"Value of B2","C":"Value of C2"},
  {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜