ASP.NET Core Web API Interview Question #42
What is ActionResult<T> in ASP.NET Core Web API?
DTOs, Model Binding, Validation & API Responses Junior Beginner
Quick Interview Answer
ActionResult<T> allows an action to return either a typed success value or another ActionResult such as NotFound, BadRequest, or Forbid. It communicates the successful response type clearly while retaining flexible HTTP error handling.
Detailed Explanation
ActionResult<T> allows an action to return either a typed success value or another ActionResult such as NotFound, BadRequest, or Forbid. It communicates the successful response type clearly while retaining flexible HTTP error handling.
It is especially useful for API actions because tools such as OpenAPI generators can reason about the primary response type more effectively.
Code Example
[HttpGet("{id:int}")]
public async Task<ActionResult<ProductResponse>> Get(
int id,
CancellationToken token)
{
var product = await _service.GetAsync(id, token);
if (product == null)
return NotFound();
return product;
}