无法在控制器中 return badrequest mvc

Cannot return badrequest mvc in the controller

为什么我不能 return 在这里提出错误的请求?在其他示例中,它工作得很好。

[HttpGet("getPrediction/{dateTime}/{filterUnit}/{filterLocation}/{filterContent}")]
public int GetFillLevelForDateTime(string dateTime, bool filterUnit, int unit, bool filterLocation,
    double latitude, double longitude, bool filterContent, string containerContent)
{
    try
    {
        bool tempExists = predictionService.CheckIfMessagesExist(unit, latitude, longitude, filterUnit, filterLocation);
        if (tempExists)
        {
            return GetFillLevelPrediction(dateTime, filterUnit, unit, filterLocation, latitude, longitude, filterContent, containerContent);
        }
    } catch (Exception ex)
    {
        return BadRequest();
    }
}

你的 GET 方法 return 值是整数,BadRequest 是对象类型,我建议使用 IActionResult 作为 return 值

 public IActionResult GetFillLevelForDateTime(string dateTime, bool filterUnit, int unit, bool filterLocation,
    double latitude, double longitude, bool filterContent, string containerContent)
{
    try
    {
        bool tempExists = predictionService.CheckIfMessagesExist(unit, latitude, longitude, filterUnit, filterLocation);
        if (tempExists)
        {
            return Ok(GetFillLevelPrediction(dateTime, filterUnit, unit, filterLocation, latitude, longitude, filterContent, containerContent));
        }
    } catch (Exception ex)
    {
        return BadRequest();
    }
}

改变你的行动header

  public ActionResult<int> GetFillLevelForDateTime ....