【问题标题】:How can we pass the more than One class objects to WCF REST API?我们如何将多个类对象传递给 WCF REST API?
【发布时间】:2014-06-23 23:57:26
【问题描述】:

我有一个 WCF REST API 方法,它接受两个类对象 (RequestFormat = JSON) 作为输入。我知道传递单个对象的过程。任何人都可以帮助我在 WCF Rest API 方法中传递多个对象作为输入的过程。

【问题讨论】:

    标签: asp.net wcf rest wcf-rest


    【解决方案1】:

    您需要将消息正文样式设置为已包装:WebMessageBodyStyle.Wrapped。

    这是一个例子:

    数据模型:

    public class ServiceResult
    {
        public string ResultCode { get; set; }
    }
    
    public class User
    {
        public string UserId { get; set; }
        public string Name { get; set; }
        public string Surename { get; set; }
    }
    
    public class Account
    {
        public string AccNumber { get; set; }
        public bool IsActive { get; set; }
    }
    

    服务接口方法:

    [OperationContract]
    [WebInvoke(UriTemplate = "user/details", Method = "POST", RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    ServiceResult Details(User user, string key, Account account);
    

    请求数据的代码:

    const string json = @"{
            ""user"": 
                {
                    ""UserId"":""12"",
                    ""Name"":""Bogdan"",
                    ""Surename"":""Perecotypole""
                },
            ""key"": ""12345"",
            ""account"": 
                {
                ""AccNumber"":""ED12"",
                ""IsActive"":""true""
                }
            }";
    
    Uri uri= new Uri("http://localhost/user/details");
    var wc = new WebClient();
    wc.Headers["Content-Type"] = "application/json";
    
    var resJson = wc.UploadString(uri, "POST", json);
    

    【讨论】:

      猜你喜欢
      • 2014-06-24
      • 2014-01-06
      • 1970-01-01
      • 2019-10-21
      • 2016-12-20
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      相关资源
      最近更新 更多