wasAsked.

What is the difference between IActionResult and ActionResult<T> in ASP.NET Core?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · ASP.NET Core, .NET

Community answers

Posted by wasAsked editorial collection

Both are return types for ASP.NET Core controller actions, but ActionResult<T> adds type information about the success response. IActionResult - An interface representing any HTTP result: Ok(), NotFound(), BadRequest(), Created(), NoContent(), File() and so on. - The action can return different result types, but the method signature does not say what data type a successful response contains. [HttpGet("{id}")] public async Task<IActionResult> GetProduct(int id) { var product = await _db.Products.FindAsync(id); if (product is null) return NotFound(); return Ok(product); } - Swagger/OpenAPI cannot infer the response body type, so you add [ProducesResponseType(typeof(Product), StatusCodes.Status200OK)] attributes for documentation. ActionResult<T> - Introduced in ASP.NET Core 2.1. It can hold either a T value or any ActionResult. - Allows returning the object directly — it is implicitly converted to a 200 OK response — while still allowing status-code results. [HttpGet("{id}")] public async Task<ActionResult<Product>> GetProduct(int id) { var product = await _db.Products.FindAsync(id); if (product is null) return NotFound(); return product; // implicitly Ok(product) } Benefits of ActionResult<T> - Clear intent: the signature shows the success type. - OpenAPI tools infer the 200 response schema automatically (you still document other status codes, for example with [ProducesResponseType(StatusCodes.Status404NotFound)]). - Less code: no need to wrap the success value in Ok(). - Compile-time checking: returning the wrong type for the success case is caught by the compiler. Limitations - Implicit conversion does not work for interfaces: ActionResult<IEnumerable<Product>> cannot be returned directly from an IEnumerable<T> variable expression in some cases; convert to a concrete type (ToList()) or use Ok(...). - For actions that return different success types, or no body at all (only NoContent), IActionResult may be simpler. When to use which - ActionResult<T>: most API GET endpoints and any endpoint returning a specific model on success. - IActionResult: actions without a single success type, file downloads, redirects, or endpoints that only return status codes. Minimal APIs equivalent Minimal APIs use IResult and TypedResults. For full type information in OpenAPI, return union types: app.MapGet("/products/{id}", async Task<Results<Ok<Product>, NotFound>> (int id, AppDbContext db) => await db.Products.FindAsync(id) is { } p ? TypedResults.Ok(p) : TypedResults.NotFound()); Results<T1, T2> documents every possible response automatically. Related types - ActionResult (non-generic) is the base class implementing IActionResult. - ObjectResult, OkObjectResult, NotFoundResult, CreatedAtActionResult and others are concrete implementations. - [ApiController] adds automatic 400 responses for invalid models and problem details for errors. Interview summary: "IActionResult can represent any result but hides the success type. ActionResult<T> lets you return either T (implicitly 200 OK) or other results like NotFound, giving clearer signatures, better OpenAPI documentation and compile-time checks. In minimal APIs, TypedResults with Results<...> serve the same purpose."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics