Вопрос

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