Search code examples
c#.netasp.net-core

IActionResult vs IResult ASP.NET Core


What is the difference between Microsoft.AspNetCore.Http.IResult and Microsoft.AspNetCore.Mvc.IActionResult? Which one should I use when I'm going to build an API? I usually use the Results class with its methods for that.


Solution

  • IActionResult vs IResult

    IActionResult

    IActionResult is part of the MVC framework. It is found in Microsoft.AspNetCore.Mvc namespace in the Microsoft.AspNetCore.Mvc.Abstractions.dll library. Commonly it is used in MVC-based web API implementations. This has existed for quite some time, since around when ASP.NET MVC was first started. It exists in the .NET Framework, and all versions of .NET Core / .NET.

    Microsoft Learn: IActionResult Interface

    IResult

    IResult is part of the new API framework introduced as part of .NET 6.0. It is found in the Microsoft.AspNetCore.Http namespace in the Microsoft.AspNetCore.Http.Abstractions.dll library. IResult is fully supported with the new minimal APIs features of .NET 6.0+.

    Microsoft Learn: IResult Interface

    Do they inherit from each other?

    No, these interfaces are completely separate, coming from separate namespaces and libraries.

    When should I use one vs the other?

    This depends, but generally, if you are working with an MVC-based API controller, you should consider using IActionResult (or the base ActionResult class, if desired).

    If you are writing APIs without MVC using .NET 6.0+, either using the Minimal API framework or something else, you should consider using IResult.

    Consider using the framework-provided IResult-derived classes (TypedResults or Results) instead of the IResult interface directly 1.

    Additional Microsoft Learn Resources

    Here are some excellent resources from Microsoft Learn that provide overviews and helpful insights into each of these areas of API development.

    Microsoft Learn: Controller action return types in ASP.NET Core web API

    Microsoft Learn: ASP.NET Core Fundamentals Overview

    Microsoft Learn: Tutorial: Create a minimal API with ASP.NET Core

    Microsoft Learn: Tutorial: Create a web API with ASP.NET Core

    Microsoft Learn: Overview of ASP.NET Core MVC