我试图利用IActionResult接口抛出特定的异常。但此刻我被困在控制器上。我编写了以下代码来获得一个调用:
[Route("singlecall")]
[HttpGet]
[ProducesResponseType(200, Type = typeof(Models.CallModel))]
[ProducesResponseType(404)]
public async Task<IActionResult> GetSingleCall([FromQuery]string callId, [FromQuery] string token)
{
CallModel singleCall = await CustomerPortalBC.CallManagementBusiness.CallService.GetSingleCall(new Guid(callId), new Guid(token));
if (singleCall == null){
return NotFound();
}
return singleCall;
}
这(显然)不起作用,因为我不能只返回singleCall对象。它期望一个Microsoft.AspNetCore.Mvc.IActionResult。所以我确切地知道问题是什么,我只是不知道如何解决它。
任何见解或帮助将不胜感激!
你想要return Ok(singleCall);
请试试:
return Ok(singleCall);
您的对象将是正确的HTTP结果。
您还可以返回自定义消息。
catch (Exception ex)
{
return Json(new { status = "error", message = ex.InnerException });
}