asp.net 核心 web api 如何从客户端 c# 发送 json 对象
asp.net core web api how to send json object from client side c#
这是我的客户端代码
var client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
var multipart = new MultipartFormDataContent();
var jsonToSend = JsonConvert.SerializeObject(template, Formatting.None);
var body = new StringContent(jsonToSend, Encoding.UTF8, "application/json");
multipart.Add(body, "JsonDetails");
return client.PostAsync("jsons", multipart);
服务器代码是
[HttpPost("jsons")]
public async Task<IActionResult> RequestJson([FromBody]Person person)
{
if (person != null)
{
return Ok("true");
}
return Ok("false");
个人代码
public class Person
{
public string Name { get; set; }
public string Position { get; set; }
}
当调试我的 post 从客户端不敲服务器时,在 postman 我的 post 发送中我可以看到我的对象 属性
直接 post StringContent 而不是 Multipart-Form:
var client = new HttpClient
{
BaseAddress = new Uri(BASE_URL)
};
var jsonToSend = JsonConvert.SerializeObject(template, Newtonsoft.Json.Formatting.None);
var body = new StringContent(jsonToSend, Encoding.UTF8, "application/json");
return client.PostAsync("jsons", body)
这是我的客户端代码
var client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
var multipart = new MultipartFormDataContent();
var jsonToSend = JsonConvert.SerializeObject(template, Formatting.None);
var body = new StringContent(jsonToSend, Encoding.UTF8, "application/json");
multipart.Add(body, "JsonDetails");
return client.PostAsync("jsons", multipart);
服务器代码是
[HttpPost("jsons")]
public async Task<IActionResult> RequestJson([FromBody]Person person)
{
if (person != null)
{
return Ok("true");
}
return Ok("false");
个人代码
public class Person
{
public string Name { get; set; }
public string Position { get; set; }
}
当调试我的 post 从客户端不敲服务器时,在 postman 我的 post 发送中我可以看到我的对象 属性
直接 post StringContent 而不是 Multipart-Form:
var client = new HttpClient
{
BaseAddress = new Uri(BASE_URL)
};
var jsonToSend = JsonConvert.SerializeObject(template, Newtonsoft.Json.Formatting.None);
var body = new StringContent(jsonToSend, Encoding.UTF8, "application/json");
return client.PostAsync("jsons", body)