문제

I have a WCF REST API method which accepts two class objects (RequestFormat = JSON ) as inputs . I know the process of passing the single object. Can anyone help me the process of passing more than one object as inputs in WCF Rest API method.

도움이 되었습니까?

해결책

You need to set message body style as wrapped: WebMessageBodyStyle.Wrapped.

Here is an example:

Data model:

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; }
}

Service interface method:

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

Code to request data:

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top