ASP.NET Core MVC Interview Question #6
What is IActionResult in ASP.NET Core MVC?
MVC Fundamentals, Controllers, Views & Routing Junior Beginner
Quick Interview Answer
IActionResult is an abstraction representing the result of an MVC action. Different implementations describe different HTTP responses, including ViewResult, RedirectToActionResult, NotFoundResult, FileResult, JsonResult, and ContentResult.
Detailed Explanation
IActionResult is an abstraction representing the result of an MVC action. Different implementations describe different HTTP responses, including ViewResult, RedirectToActionResult, NotFoundResult, FileResult, JsonResult, and ContentResult.
Using IActionResult is useful when an action may return different result types depending on conditions. ASP.NET Core also supports ActionResult<T> primarily in API-style scenarios and allows actions to return certain concrete types directly.
Code Example
public IActionResult Details(int id)
{
if (id <= 0)
return BadRequest();
return View();
}