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 modelRedirectToAction()— PRG pattern after POSTNotFound(),BadRequest()— HTTP errorsOk(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
- Q: Controller vs ControllerBase?
A: Controller adds View helpers; API projects use ControllerBase to avoid view dependencies. - Q: Fat controller smell?
A: Move business logic to services; controllers orchestrate HTTP only.
Self-check
- What return type renders a Razor page?
- When use RedirectToAction after POST?