ASP.NET Core Interview Question #108

What is a controller in ASP.NET Core MVC?

Routing, Controllers & Model Binding Mid-Level Intermediate

Quick Interview Answer

A controller is a class that participates in handling HTTP requests in the MVC pattern. It receives input, coordinates application services, and returns an action result.

Detailed Explanation

A controller is a class that participates in handling HTTP requests in the MVC pattern. It receives input, coordinates application services, and returns an action result.

MVC controllers usually inherit from Controller, which provides view-related helpers and access to MVC features. API controllers often inherit from ControllerBase when they do not need view support.

Controllers should remain thin. Business rules, data access, and complex application logic are normally delegated to services or other application-layer components.

Code Example

public class ProductsController : Controller
{
    private readonly IProductService _products;

    public ProductsController(IProductService products)
    {
        _products = products;
    }

    public IActionResult Index()
    {
        return View();
    }
}