【问题标题】:One application hits a request in another but the information is empty一个应用程序在另一个应用程序中遇到请求,但信息为空
【发布时间】:2021-01-18 01:54:51
【问题描述】:
 public async Task Execute(IJobExecutionContext context)
        {
            var result = this.hardwareInfoManager.GetHardWareInfo();
            Log.Logger.Information(JsonConvert.SerializeObject(result));
            using (HttpClient client = new HttpClient())
            {
                var url = Configuration.GetValue<string>("HealthMonitorApplicationUrl");
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
                var serializedString = JsonConvert.SerializeObject(result);
                request.Content = new StringContent(serializedString, Encoding.UTF8, "application/json");
                var response = await retryPolicy.ExecuteAsync(async () => await client.PostAsync(url, request.Content));
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Log.Logger.Information(response.StatusCode.ToString());
                    if (response.Content != null)
                    {
                        Log.Logger.Information(await response.Content.ReadAsStringAsync());
                    }
                }
            }
        }

我向 url "http://localhost:5002/api/v1/healthmonitor" 发送请求。

它命中了另一个应用程序中的方法:

    [Route("/api/v1/healthmonitor")]
    [HttpPost]
    public async Task<IActionResult> ReceiveHardwareInfo(HardwareInfoDto hardwareInfoDto)
    {
        var result = await hardwareInfoManager.SaveHardwareInfo(hardwareInfoDto);
        if (result.Succeeded)
        {
            return Ok(result);
        }
        else 
        {
            return BadRequest($"{result.Errors}");
        }
    }

但没有填充任何信息。我也尝试过使用 Postman,但无济于事。

你能建议吗?

【问题讨论】:

  • 这里只是走弯路,如果你把ReceiveHardwareInfo(HardwareInfoDto hardwareInfoDto)改成ReceiveHardwareInfo(object hardwareInfoDto)会怎么样
  • 不幸的是还是空的
  • 如果您删除 [Route("/api/v1/healthmonitor")] 标记并点击该端点会怎样?

标签: c# asp.net-core httprequest response


【解决方案1】:

解决了。将控制器中的方法改成这样:

    public async Task<IActionResult> ReceiveHardwareInfo([FromBody] HardwareInfoDto hardwareInfoDto)

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多