Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

controllers-actions

Controllers and actions

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Controllers and actions: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship Controllers and actions on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Controllers and actions in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Controllers handle HTTP requests and return responses—HTML views, redirects, or JSON. Each public method is an action. MVC controllers inherit Controller; API controllers use ControllerBase with [ApiController].

Basic MVC controller

public class HomeController : Controller {
    public IActionResult Index() => View();
    public IActionResult About() => View();
}

Returning different results

  • View() — Razor view with optional model
  • RedirectToAction() — PRG pattern after POST
  • NotFound(), BadRequest() — HTTP errors
  • Ok(dto) — JSON 200 in APIs

Action parameters

Route, query, and form values bind to parameters by name—Details(int id) gets id from route or query with model binding.

Important interview questions and answers

  1. Q: Controller vs ControllerBase?
    A: Controller adds View helpers; API projects use ControllerBase to avoid view dependencies.
  2. Q: Fat controller smell?
    A: Move business logic to services; controllers orchestrate HTTP only.

Self-check

  1. What return type renders a Razor page?
  2. When use RedirectToAction after POST?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • IActionResult types?
  • FromBody binding?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump