这是我的客户代码
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; }
}
当从客户端调试我的帖子时,不要敲服务器,在邮递员中,我的帖子发送并且我可以看到我的对象属性
答案 0 :(得分:1)
只需直接发布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)