ASP.NET Core Interview Question #112

What are common ActionResult types in ASP.NET Core MVC?

Routing, Controllers & Model Binding Mid-Level Intermediate

Quick Interview Answer

Common MVC results include ViewResult for rendering a view, RedirectResult and RedirectToActionResult for redirects, JsonResult for JSON, ContentResult for text, FileResult for files, and status-code results such as NotFoundResult or BadRequestResult.

Detailed Explanation

Common MVC results include ViewResult for rendering a view, RedirectResult and RedirectToActionResult for redirects, JsonResult for JSON, ContentResult for text, FileResult for files, and status-code results such as NotFoundResult or BadRequestResult.

Controller and ControllerBase provide helper methods that create these results, including View(), RedirectToAction(), Ok(), BadRequest(), NotFound(), File(), and Content().

The chosen result should accurately communicate the outcome of the request.

Code Example

public IActionResult Download()
{
    byte[] data = System.Text.Encoding.UTF8.GetBytes("Report");
    return File(data, "text/plain", "report.txt");
}

public IActionResult Missing()
{
    return NotFound();
}