Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Async Task <IActionResult> return Object

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!

like image 436
zerk Avatar asked Jan 16 '19 12:01

zerk


2 Answers

You want to return Ok(singleCall);

like image 188
nvoigt Avatar answered Oct 11 '22 17:10

nvoigt


Please try:

return Ok(singleCall);

Your object will be then proper HTTP result.

like image 39
Maciej S. Avatar answered Oct 11 '22 16:10

Maciej S.