gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:11 25 4
gpt4 key购买 nike

我目前正在像这样发布到我的网络 api:

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

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

如果这不可能,我将不得不为此目的制作一个具有 2 个属性的模型:

IList<Product> productList
string additionalParam

最佳答案

好的,我得到了一个不使用 PostAsJsonAsync 的示例:

设置:

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

和 Controller :

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

显然这适用于我的示例对象 somemodel 但你明白了。

关于c# - 将多个参数传递给 client.PostAsJsonAsync 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com