ASP.NET Core Interview Question #111
What is IActionResult?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
IActionResult is an abstraction representing the result of an MVC action. Different implementations can produce views, redirects, JSON responses, files, status codes, or other HTTP responses.
Detailed Explanation
IActionResult is an abstraction representing the result of an MVC action. Different implementations can produce views, redirects, JSON responses, files, status codes, or other HTTP responses.
Using IActionResult is useful when an action can return different result types depending on the outcome. For example, an action might return NotFound() when a record does not exist and View(model) when it does.
For APIs that return a strongly typed response plus possible HTTP error results, ActionResult<T> is often useful.
Code Example
public IActionResult Details(int id)
{
var product = _service.Get(id);
if (product == null)
return NotFound();
return View(product);
}