【发布时间】:2014-06-23 23:57:26
【问题描述】:
我有一个 WCF REST API 方法,它接受两个类对象 (RequestFormat = JSON) 作为输入。我知道传递单个对象的过程。任何人都可以帮助我在 WCF Rest API 方法中传递多个对象作为输入的过程。
【问题讨论】:
我有一个 WCF REST API 方法,它接受两个类对象 (RequestFormat = JSON) 作为输入。我知道传递单个对象的过程。任何人都可以帮助我在 WCF Rest API 方法中传递多个对象作为输入的过程。
【问题讨论】:
您需要将消息正文样式设置为已包装: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);
【讨论】: