William Venice
William Venice

Reputation: 329

Passing multiple parameters to client.PostAsJsonAsync function

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

Upvotes: 4

Views: 17976

Answers (1)

Ric
Ric

Reputation: 13248

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.

Upvotes: 4

Related Questions