在控制器函数中返回NotFound()方法时出现CS0029错误

问题描述 投票:0回答:1

如何在控制器函数中处理此错误?

返回 NotFound() 方法时出现 CS0029 错误...

    [HttpGet("{id}")]
    public async Task<Database?> GetById(string id)
    {
        var response = await _databasesService.GetById(id);

        var database = response.FirstOrDefault();

        if (database is not null)
            return database;
        else
            return NotFound(); // Error CS0029
    }

c# asp.net asp.net-mvc asp.net-core asp.net-web-api
1个回答
1
投票

您的方法需要

Database?
类型的返回类型,因此您无法将
NotFound
对象强制转换为该类型。要解决此问题,请更改方法签名以返回
Task<ActionResult<Database>>
,例如:

public async Task<ActionResult<Database>> GetById(string id)
{
   ...
}
© www.soinside.com 2019 - 2024. All rights reserved.