locked
How to pass multiple complex objects to Web API action RRS feed

  • Question

  • User264732274 posted

    suppose this is sample action

    public void StartProcessiong([FromBody]Content content, [FromBody]Config config)
    {
    
    }
    
    OR
    
    public void StartProcessiong(Content content, Config config)
    {
    
    }

    client code
    -----------
    using (var httpClient = new System.Net.Http.HttpClient())
    {
        httpClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["Url"]);
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));   

        var response = httpClient.PutAsync("api/process/StartProcessiong", objectA, objectB);
    }

    taking code from http://stackoverflow.com/questions/24874490/pass-multiple-complex-objects-to-a-post-put-web-api-method

    how to use http client to pass multiple complex object too....need sample code. thanks

    Tuesday, September 13, 2016 9:13 AM

Answers

  • User765422875 posted

    Got it. This limitation is by design. 

    https://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    "When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

    At most one parameter is allowed to read from the message body. The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once."

    You can look into some of the converter options., though not sure why you are opposed to a composite object or JObject.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 14, 2016 3:38 PM

All replies

  • Tuesday, September 13, 2016 3:47 PM
  • User36583972 posted

    Hi sudip_inn,

    You provide a link and it explains clearly that only one complex object can be parsed from the body for one request.

    You can refer the following article about How WebAPI does Parameter Binding:

    https://blogs.msdn.microsoft.com/jmstall/2012/04/16/how-webapi-does-parameter-binding/

    You can consider other ways to pass multiple complex objects to Web API action. You can refer the following code example.

    Client:

                HttpClient client = new HttpClient();
                Uri baseAddress = new Uri("http://localhost:29679/");
                client.BaseAddress = baseAddress;
                ArrayList paramList = new ArrayList();
                Individual Individual1 = new Individual
                {
                    Address = "aa",
                    FirstName = "b",
                    Address1 = "hell",
                    Surname = "qwq"
                };
                Individual Individual2 = new Individual
                {
                    Address = "aa2",
                    FirstName = "b2",
                    Address1 = "hell2",
                    Surname = "qwq2"
                };
                paramList.Add(Individual1);
                paramList.Add(Individual2);
                HttpResponseMessage response = client.PostAsJsonAsync("api/Values", paramList).Result;
                if (response.IsSuccessStatusCode)
                {
                }

    Web API:

            [HttpPost]
            [AllowAnonymous]
            public HttpResponseMessage Post(ArrayList paramList)
            {
               
            }
    

    Best Regards,

    Yohann Lu

    Wednesday, September 14, 2016 4:47 AM
  • User264732274 posted

    thanks for answer.

    i do not like array list rather i like to use viewmodel or JObject but i think there is way to pass multiple object without arraylist or viewmodel.

    please show that to me to pass multiple parameter without using arraylist or viewmodel.post action code and client code too.

    thanks

    Wednesday, September 14, 2016 8:47 AM
  • User765422875 posted

    As I said, you can pass an object.

    public class CompositeObject
    {
        public Content Content { get; set; }
        public Config Config { get; set; }
    }
    public void StartProcessiong([FromBody] CompositeObject composite)
    { ... }

    See sections Creating a Resource (HTTP POST)  & Updating a Resource (HTTP PUT)

    https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Wednesday, September 14, 2016 2:44 PM
  • User264732274 posted

    i want to send 2 objects separately not combined in any view model like approach.

    Wednesday, September 14, 2016 2:59 PM
  • User765422875 posted

    Got it. This limitation is by design. 

    https://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    "When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

    At most one parameter is allowed to read from the message body. The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once."

    You can look into some of the converter options., though not sure why you are opposed to a composite object or JObject.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 14, 2016 3:38 PM