ActionResult<IEnumerable<Model>> vs IActionResult in ASP.NET Core WebAPI

Shervan360 1,601 Reputation points
2024-10-03T07:36:52.1066667+00:00

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);
          }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,626 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
345 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,016 Reputation points
    2024-10-03T15:30:13.3266667+00:00

    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.

    0 comments No comments

  2. Ping Ni-MSFT 4,720 Reputation points Microsoft Vendor
    2024-10-04T01:37:52.0066667+00:00

    Hi @Shervan360,

    IActionResult type

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

    ActionResult type

    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:

    • The [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>.
    • Implicit cast operators support the conversion of both 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;
        }
      

    Reference

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.