Asp.net core 我有两个ActionResult返回类型的方法,但其中一个返回单个Employee对象,并且仍然有效。为什么呢?
我刚刚在Blazor应用程序中编写了一个控制器类,我不了解什么。 我有一个方法GetEmployee:Asp.net core 我有两个ActionResult返回类型的方法,但其中一个返回单个Employee对象,并且仍然有效。为什么呢?,asp.net-core,.net-core,Asp.net Core,.net Core,我刚刚在Blazor应用程序中编写了一个控制器类,我不了解什么。 我有一个方法GetEmployee: [HttpGet("{employeeId:int}")] public async Task<ActionResult<Employee>> GetEmployee(int employeeId) { try { var result =
[HttpGet("{employeeId:int}")]
public async Task<ActionResult<Employee>> GetEmployee(int employeeId)
{
try
{
var result = await employeeRepsitory.GetEmployee(employeeId);
if (result == null)
{
NotFound();
}
return result;
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database");
}
}
将对象放入Ok()
ActionResult。如果我没有在Ok()
中包装wait employeerepository.GetEmployees()
,那么我有编译错误,因为我没有返回ActionResult。但是,这个GetEmployee(int-employeeId)
方法如何不给我编译错误,但我显然是在返回Employee对象,而不是ActionResult。谢谢你的回答
[HttpGet]
public async Task<ActionResult<IEnumerable<Employee>>> GetEmployees()
{
try
{
return Ok(await employeeRepsitory.GetEmployees());
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database");
}
}