【问题标题】:How to post a List<T> to a WCF Rest service如何将 List<T> 发布到 WCF Rest 服务
【发布时间】:2011-06-16 14:08:07
【问题描述】:

我有课

[DataContract]
public class Test
{
[DataMemeber]
public string A {get;set;}
[DataMemeber]
public string B {get;set;}
[DataMemeber]
public string C {get;set;}
}

我有一个 Restful WCF 方法

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

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

如何将 List 对象发布到服务?这是来自 wpf 客户端。

谢谢

【问题讨论】:

    标签: wcf rest post service


    【解决方案1】:

    List 相当于一个数组,所以值应该表示为 JSON 数组。并且由于 body 样式表明请求需要被包装,那么您应该将 JSON 数组包装在一个对象中,其中包含一个名为参数的字段:

    {"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"}]}
    

    如果请求没有被包装(Bare 的 BodyStyle 或 WrappedResponse),那么你就不需要包装对象,这将是一个操作请求:

    [
      {"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"}
    ]
    

    【讨论】:

    • 能不能给我举个例子。改变 BodyStyle 会让生活更轻松吗?
    • 如果 BodyStyle 是 Bare(或 WrappedResponse,表示请求未包装),那么您不需要包装对象 - 我将使用 Bare case 的请求更新答案。
    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多