one is strong typed and will give compile error if return value does not match. also via reflection the return type can be deduced say for swagger.
the other just returns any object.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
What is the difference between ActionResult<IEnumerable<SpeakerModel>> vs IActionResult ?
I ran both of them and got the same result.
public IActionResult GetAll()
{
return Ok(SpeakersList);
}
[HttpGet]
public ActionResult<IEnumerable<SpeakerModel>> GetAll()
{
return Ok(SpeakersList);
}
one is strong typed and will give compile error if return value does not match. also via reflection the return type can be deduced say for swagger.
the other just returns any object.
Hi @Shervan360,
This solves the problem above as the IActionResult
return type covers different return types.
public IActionResult Get() {
SpeakerModel model= ...;
if(model== null)
{
return NotFound();
}
else
{
return Ok(model);
}
}
ASP.NET Core includes the ActionResult<T> return type for web API controller actions. It enables returning a type deriving from ActionResult or return a specific type. ActionResult<T>
offers the following benefits over the IActionResult type:
[ProducesResponseType]
attribute's Type
property can be excluded. For example, [ProducesResponseType(200, Type = typeof(SpeakerModel ))]
is simplified to [ProducesResponseType(200)]
. The action's expected return type is inferred from the T
in ActionResult<T>
.T
and ActionResult
to ActionResult<T>
. T
converts to ObjectResult, which means return new ObjectResult(T);
is simplified to return T;
.
public ActionResult<SpeakerModel > Get()
{
SpeakerModel model= ...;
if(model== null){
return NotFound();
}
return model;
}
Controller action return types in ASP.NET Core web API
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Rena