liveBook · Manning

ControllerBase

As explained in ASP.NET Core in Action, 3e

The Controller base class is the ASP.NET Core MVC base class that provides controller infrastructure, including access to an IUrlHelper.

ControllerBase is the usual base class for ASP.NET Core web API controllers. It supplies controller infrastructure and helper methods for generating HTTP results, without the Razor view features associated with the MVC Controller class. section 20.1

A ControllerBase-derived controller typically works with:

  • [ApiController], which applies common web API conventions. section 20.1
  • Action methods that return data directly or return ActionResult values for conditions such as not found. section 20.1
  • Routing attributes such as [HttpGet("fruit")] to expose actions as HTTP endpoints. section 20.1

A small web API controller

Declare `public class FruitController : ControllerBase`, add `[ApiController]`, and define an `[HttpGet("fruit")]` action returning `IEnumerable<string>`. A GET request to `/fruit` returns the list as JSON with a 200 OK response. An action returning `ActionResult<string>` can instead call `NotFound()` when an identifier is invalid.

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the base class, helper methods, inheritance choice, routing, serialization, and basic success and error responses.
Examples
Demonstrates FruitController, a fruit collection, GET routing, JSON serialization, and NotFound handling.
What it assumes
Assumes basic C# classes, methods, collections, and HTTP concepts while establishing controller structure through a small example.
What you can do after
Build a small web API controller that returns serialized data and produces an appropriate error result.
How it connects
Links ControllerBase with ApiController, action methods, routing, ActionResult, MVC controllers, Razor Pages, and minimal APIs.

Also explained in

Compare how 2 books explain ControllerBase →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.