8

I am trying to make usage of the IActionResult interface to throw specific exceptions. But at this moment I am stuck at the controller. I've written the following code to get a single call:

    [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;
    }

This will (obviously) not work, because I cannot just return the singleCall object. It expects a Microsoft.AspNetCore.Mvc.IActionResult. So I exactly know what the problem is, I only do not know how to solve it.

Any insight or help would be appreciated!

zerk
  • 516
  • 4
  • 9
  • 34

3 Answers3

6

You want to return Ok(singleCall);

nvoigt
  • 75,013
  • 26
  • 93
  • 142
4

Please try:

return Ok(singleCall);

Your object will be then proper HTTP result.

Maciej S.
  • 752
  • 10
  • 22
0

You can also return a custom message.

catch (Exception ex)
{
   return Json(new { status = "error", message = ex.InnerException });
}
Dwhitz
  • 1,250
  • 7
  • 26
  • 38
hardik patel
  • 221
  • 4
  • 8