将多个参数传递给client.PostAsJsonAsync函数

时间:2015-10-09 14:03:33

标签: c# asp.net asp.net-web-api2 asp.net-web-api

我目前正在发布这样的网址:

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

我想知道除了我的Product对象列表之外,我还可以在控制器中向该方法发送一个额外的字符串,而不会将其作为我的Product模型的属性。

如果无法做到这一点,我将不得不制作一个只有2个属性的模型:

IList<Product> productList
string additionalParam

1 个答案:

答案 0 :(得分:2)

好的,所以我在没有使用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 的情况下工作了一个样本:

设置:

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

控制器:

somemodel

显然这对我的样本对象Kitkat有效,但你明白了。

相关问题