【发布时间】:2019-06-09 02:26:59
【问题描述】:
我正在尝试使用 web api 创建一个示例。我希望接收到的 JSON 参数为 fromuri 或 frombody。当我创建客户端时,我想发送一个包含信息的对象或 JSON。
在我的网络 API 中有这个
[HttpPost]
public IHttpActionResult Post([FromUri]Peticion peticion)
{
return Ok(peticion);
}
在我的客户中
Peticion obj = new Peticion();
obj.cliente = 2;
obj.factura = 22;
string DATA = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
var client = new HttpClient();
HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json");
HttpResponseMessage message =
client.PostAsync("http://localhost:57418//api/Deudor/",content).Result;
if (message.IsSuccessStatusCode)
{
string result = message.Content.ReadAsStringAsync().Result;
}
但我需要在我的客户端收到 JSON。在 Web API 中,我需要接收两个参数(cliente、factura),而在我的客户端中,我需要以 JSON 格式接收该客户端的信息(名称、发票等)。
我使用了邮递员,并收到了带有信息的 JSON。但在我的客户端 C# 中我不能。
【问题讨论】:
标签: c# asp.net json asp.net-web-api asp.net-web-api2