我有一个WCF REST API方法,它接受两个类对象(RequestFormat = JSON)作为输入。我知道传递单个对象的过程。任何人都可以帮助我在WCF Rest API方法中传递多个对象作为输入的过程。
答案 0 :(得分:2)
您需要将邮件正文样式设置为包装: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);