4

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
5
  • Can you add another parameter to your controller method? Commented Oct 9, 2015 at 14:09
  • Yes for example: public HttpResponseMessage MyMethod(IList<Product> productList, string additionalParam). But how would I fill this param from my other application when calling PostAsJsonAsync. Commented Oct 9, 2015 at 14:14
  • 1
    you could append one to the URI ie api/controller?additionalParam=something and the other using from body attribute. asp.net/web-api/overview/formats-and-model-binding/…, so it would look like: client.PostAsJsonAsync("api/controller/methodincontroller?additionalParam=" + myParam, listOfProductObject); Commented Oct 9, 2015 at 14:20
  • So on the receiving end how would my method declaration look? Which param comes first and do I have to change any of my route settings? Commented Oct 9, 2015 at 14:28
  • See below, note that I am not using PostAsJsonAsync, but the idea is the same. Commented Oct 9, 2015 at 14:52

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This works perfect. My controller code looks exactly like yours, but the code in the setup section is like this: Task<HttpResponseMessage> response = client.PostAsJsonAsync("api/mycontroller/controllermethod?additionalParam=" + "test", productList); Is there any particular reason you added additional code in the setup section? I am pretty sure my productList automatically gets converted to Json when sent. In the debugger what I currently have seems to be working.
Yeah because I don't seem to have PostAsJsonAsync and nuget isn't allowing me to get it for some reason at the moment, I just added some code to get it working and to pass in the json, but it's basically the same thing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.