ASP.NET Core Interview Question #109

What is the difference between Controller and ControllerBase?

Routing, Controllers & Model Binding Mid-Level Intermediate

Quick Interview Answer

ControllerBase provides the core functionality needed for HTTP endpoints, including model state, status-code helpers, request information, and action result helpers. It is commonly used for APIs.

Detailed Explanation

ControllerBase provides the core functionality needed for HTTP endpoints, including model state, status-code helpers, request information, and action result helpers. It is commonly used for APIs.

Controller inherits from ControllerBase and adds support for MVC views, including methods such as View(), PartialView(), and ViewComponent().

Use Controller when rendering Razor views. Use ControllerBase for API controllers that only return data or HTTP results.

Code Example

[ApiController]
[Route("api/products")]
public class ProductsApiController : ControllerBase
{
    [HttpGet]
    public IActionResult Get() => Ok();
}

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