Passing multiple parameters to client.PostAsJsonAsync function

12,612

Ok so I got a sample working without using PostAsJsonAsync:

The setup:

string json = JsonConvert.SerializeObject(new List<somemodel> { new somemodel() });
StringContent sc = new StringContent(json, Encoding.UTF8, "application/json");

HttpClient c = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true });
var x = c.PostAsync("http://localhost:58893/api/values?additionalParam=" + "test", sc).Result; // returns 200

And the controller:

public IHttpActionResult Post([FromUri] string additionalParam, [FromBody] List<somemodel> models)

Obviously this works for my sample objects somemodel but you get the idea.

Share:
12,612
William Venice
Author by

William Venice

Updated on July 15, 2022

Comments

  • William Venice
    William Venice almost 2 years

    I am currently posting to my web api like this:

    client.PostAsJsonAsync("api/controller/methodincontroller", listOfProductObject);

    I would like to know how in addition to the list of my Product object I can also send an additional string to the method in my controller without making it a property of my Product model.

    If this isn't possible I will have to make a Model that has 2 properties for this purpose only:

    IList<Product> productList
    string additionalParam