ASP.NET Core MVC Interview Question #44
What are ContentResult and JsonResult?
Controllers, Actions, Model Binding & Action Results Junior Beginner
Quick Interview Answer
ContentResult returns text content with an optional content type and status code. JsonResult serializes an object as JSON.
Detailed Explanation
ContentResult returns text content with an optional content type and status code. JsonResult serializes an object as JSON.
Although JsonResult can be returned from an MVC controller, a substantial REST API is usually clearer when implemented with API-oriented controllers and conventions rather than mixing many API responsibilities into view controllers.
Code Example
public IActionResult Message()
{
return Content("Hello MVC");
}
public IActionResult Status()
{
return Json(new { ok = true });
}