ASP.NET Core Web API Interview Question #5

What is ControllerBase in ASP.NET Core Web API?

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

Quick Interview Answer

ControllerBase is the base class commonly used for API controllers that do not need Razor view support. It provides API-focused helpers and properties such as Ok, NotFound, BadRequest, CreatedAtAction, User, Request, and ModelState.

Detailed Explanation

ControllerBase is the base class commonly used for API controllers that do not need Razor view support. It provides API-focused helpers and properties such as Ok, NotFound, BadRequest, CreatedAtAction, User, Request, and ModelState.

Using ControllerBase keeps the controller aligned with an HTTP API instead of bringing in view-rendering functionality that the endpoint does not require.

Code Example

[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
    [HttpGet("{id:int}")]
    public IActionResult Get(int id)
    {
        return Ok(new { Id = id });
    }
}