C# 从另一个项目获取HTTP Post值
我在一个解决方案中有两个项目。我必须把第一个项目的东西发到第二个项目 我在第一个项目中的发布方法:C# 从另一个项目获取HTTP Post值,c#,http,asp.net-core,C#,Http,Asp.net Core,我在一个解决方案中有两个项目。我必须把第一个项目的东西发到第二个项目 我在第一个项目中的发布方法: [HttpPost] [Route("count")] public async Task<ActionResult> Count(DateTime date) { var section = this._configuration.GetSection("product"); using
[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();
}
}
}
在这里,内容值为空。如何获取来自第一个项目的内容值?更改StringContent,如下所示:
new StringContent(countDate, Encoding.UTF8, "application/json")
并将[FromBody]添加到第二个project post方法中:
public ActionResult Count([FromBody]string content)
Thx,我应用了您的解决方案,在这里得到了一个错误
DateTime date=(DateTime)JsonConvert.DeserializeObject(content)代码>关于时间转换。我这样修改DateTime date=DateTime.Parse(content)代码>
public ActionResult Count([FromBody]string content)