以下是一些使用ActionResult
使用ActionResult
[HttpGet("{id}")]
public ActionResult GetUser(int id)
{
User user = _userRepository.GetUser(id);
if (user == null)
{
return NotFound(); // 返回404 Not Found HTTP响应
}
return user; // 返回200 OK HTTP响应,并包含序列化的User对象
}
使用async Task
[HttpGet("{id}")]
public async Task> GetUser(int id)
{
try
{
User user = await _userRepository.GetUserAsync(id);
if (user == null)
{
return NotFound(); // 返回404 Not Found HTTP响应
}
return user; // 返回200 OK HTTP响应,并包含序列化的User对象
}
catch (Exception ex)
{
return StatusCode(500, ex.Message); // 返回500 Internal Server Error HTTP响应,并包含异常消息
}
}
在以上示例中,使用ActionResult
根据您的具体需求和偏好,您可以选择使用ActionResult