Tutorials ASP.NET Core MVC Tutorial
JsonResult — Complete Guide
JsonResult — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core MVC Tutorial on Toolliyo Academy.
On this page
ASP.NET Core MVC Tutorial · Lesson 73 of 200
JsonResult
Getting Started ✓ → Core MVC ✓ → Data & Security ✓ → Production → Career
Advanced · 7 — Performance & Deploy · ~10 min · Section 8: Action Results
What is this?
IActionResult is the return type that tells ASP.NET what to send back: HTML (View), redirect, 404, JSON, or a file download.
Why should you care?
Different situations need different responses — a form save redirects, an API call returns JSON, a missing product returns NotFound.
See it live — copy this example
Create an MVC project (dotnet new mvc), add the code, and run dotnet run.
public IActionResult Demo(int id)
{
if (id <= 0) return BadRequest();
var item = _service.Find(id);
if (item == null) return NotFound();
if (Request.Headers.Accept == "application/json")
return Json(item);
return View(item);
}
Run Example »
Edit the code and click Run — like W3Schools Try it Yourself.
What happened?
- View() renders Razor.
- RedirectToAction sends a 302 to another page.
- Json() serializes to JSON for AJAX.
- NotFound() is HTTP 404.
Try it yourself
- Return View() from Index — confirm HTML.
- Return RedirectToAction("Index") from an action — watch URL change.
- Return Json(new { ok = true }) and open URL in browser — see JSON.
- Change text or labels in the example and run again — watch the browser update.
- Break the code on purpose (remove a semicolon), read the error message, then fix it.
Remember
IActionResult = flexible response type. View, Redirect, Json, NotFound are the common ones. Pick the result that matches what the user or client expects.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!