Action results: views, JSON, redirects and files
An action decides what goes back to the browser by returning an action result. This lesson walks through the results you will use most: views, partial views, redirects, JSON, files and status codes such as 404, and when to pick each one.
- Show a pageView(model)
- Update part of a pagePartialView("_Row", model)
- After a successful POSTRedirectToAction(...)
- Data for JavaScriptJson(data)
- A downloadFile(bytes, type, name)
- Something is wrongNotFound(), BadRequest(), Forbid()
IActionResult
An action returns IActionResult (or Task<IActionResult> when it is async). The result object knows how to write the response: render a view, send JSON, set a status code or redirect. Helper methods on Controller create them, so you write return View(model) instead of building the object by hand.
The results you will use most
- View(model): render a full Razor page with the layout.
- PartialView("_Row", model): render a piece of HTML without the layout, for AJAX updates.
- RedirectToAction(nameof(Details), new { id }): send the browser to another action (302).
- Json(data): send JSON for JavaScript on the page.
- File(bytes, "application/pdf", "invoice.pdf") or PhysicalFile(path, type): send a file download.
- NotFound(), BadRequest(), Forbid(), StatusCode(503): send an HTTP status.
Example: one controller, many results
public class InvoicesController(ShopContext db, IInvoicePdf pdf) : Controller
{
public async Task<IActionResult> Details(int id)
{
var invoice = await db.Invoices.FindAsync(id);
if (invoice is null) return NotFound(); // 404
return View(invoice); // HTML page
}
public async Task<IActionResult> Download(int id)
{
var invoice = await db.Invoices.FindAsync(id);
if (invoice is null) return NotFound();
var bytes = pdf.Render(invoice);
return File(bytes, "application/pdf", $"invoice-{id}.pdf");
}
public async Task<IActionResult> Status(int id)
{
var status = await db.Invoices.Where(i => i.Id == id).Select(i => i.Status).FirstOrDefaultAsync();
return status is null ? NotFound() : Json(new { status });
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> MarkPaid(int id)
{
await db.Invoices.Where(i => i.Id == id)
.ExecuteUpdateAsync(s => s.SetProperty(i => i.Status, "Paid"));
TempData["Message"] = "Invoice marked as paid.";
return RedirectToAction(nameof(Details), new { id });
}
}Good habits
- After a successful POST, redirect (Post/Redirect/Get) so refreshing the page does not submit the form again.
- Return NotFound() for missing records instead of letting a null reference crash the view.
- Use nameof(Action) in redirects so renaming an action does not break links.
- LocalRedirect(returnUrl) only allows local URLs, which prevents open-redirect attacks.
Interview question
Why should a POST action redirect after it succeeds instead of returning a view?
This is the Post/Redirect/Get pattern. If the POST returns a view directly, refreshing the page makes the browser send the form again, which can create duplicate orders or payments. Redirecting makes the browser load a GET page, so a refresh only repeats the harmless GET. TempData can carry a success message across the redirect.
Want the full details? Read the official Microsoft documentation for this topic.