ASP.NET Core Interview Question #113

What is ActionResult<T> and when is it useful?

Routing, Controllers & Model Binding Mid-Level Intermediate

Quick Interview Answer

ActionResult<T> allows an API action to return either a strongly typed value of T or an ActionResult such as NotFound(), BadRequest(), or Unauthorized().

Detailed Explanation

ActionResult<T> allows an API action to return either a strongly typed value of T or an ActionResult such as NotFound(), BadRequest(), or Unauthorized().

This gives API endpoints clear response typing while preserving the ability to return different HTTP outcomes.

It is particularly useful with API documentation because the successful response type is explicit in the action signature.

Code Example

[HttpGet("{id:int}")]
public ActionResult<ProductDto> Get(int id)
{
    var product = _service.Get(id);

    if (product == null)
        return NotFound();

    return product;
}