ASP.NET Core Web API Interview Question #4

What is the difference between an MVC controller and an API controller?

Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner

Quick Interview Answer

An MVC controller commonly returns Razor views as well as other result types and normally derives from Controller. An API controller primarily returns data and HTTP results and commonly derives from ControllerBase.

Detailed Explanation

An MVC controller commonly returns Razor views as well as other result types and normally derives from Controller. An API controller primarily returns data and HTTP results and commonly derives from ControllerBase.

ASP.NET Core uses the same underlying MVC infrastructure for both styles. The distinction is mainly the application's endpoint purpose and the base class/features required.

Code Example

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

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