I have two projects in one solution. I have to post something from the first project to the second project.
My post method in the first project:
[HttpPost]
[Route("count")]
public async Task<ActionResult> Count(DateTime date)
{
var section = this._configuration.GetSection("product");
using (var httpClient = new HttpClient())
{
string countDate =JsonConvert.SerializeObject(date);
httpClient.BaseAddress = new Uri(section.GetValue<string>("Host"));
HttpResponseMessage responseMessage = await httpClient.PostAsync(section.GetValue<string>("Endpoint"), new StringContent(countDate));
if(responseMessage.IsSuccessStatusCode)
{
var content = await responseMessage.Content.ReadAsStringAsync();
var operationResult = JsonConvert.DeserializeObject<ResultJSON>(content);
int result = await _dataService.Counts(operationResult);
return Ok(result);
}
else
{
return BadRequest();
}
}
}
I want to send date. When I debug the code the countDate in the line of HttpResponseMessage responseMessage = await httpClient.PostAsync(section.GetValue<string>("Endpoint"), new StringContent(countDate));
is correct. When I continue to debug, it goes to second project.
My post method in the second project:
[HttpPost]
[Route("count")]
public ActionResult Count(string content)
{
DateTime date = (DateTime)JsonConvert.DeserializeObject(content);
var serviceResult = this._countingService.Count(date);
return Ok(serviceResult);
}
In here, the content value comes null. How I get the content value that comes from first project?
Change the StringContent like below:
new StringContent(countDate, Encoding.UTF8, "application/json")
And add [FromBody] to the second project post method:
public ActionResult Count([FromBody]string content)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.