liveBook · Manning

AddControllers

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

AddControllers is an ASP.NET Core service-registration method that adds the services required for controllers without adding Razor view-rendering services.

AddControllers registers the services required for ASP.NET Core web API controllers, without registering Razor view-rendering services. It is the controller-service choice for a web API project rather than an MVC application that renders views. section 20.1

Register controllers in Program.cs

WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); WebApplication app = builder.Build(); app.MapControllers(); app.Run(); AddControllers supplies the controller services; MapControllers exposes controller actions as endpoints. A controller action can then respond to requests such as GET /fruit. section 20.1

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

How deep it goes
Distinguishes controller services from Razor view services and pairs registration with MapControllers, without detailing service internals.
Examples
Uses a default web API Program.cs configuration and a FruitController responding to GET /fruit with serialized JSON.
What it assumes
Assumes an ASP.NET Core application startup file and introduces controller-only registration without requiring Razor view services.
What you can do after
Operate/configure an ASP.NET Core web API by registering controller services and mapping controller actions as endpoints.
How it connects
Connects ASP.NET Core controllers with MapControllers, explicit routing, AddControllersWithViews, Razor views, and OpenAPI services.

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.