What is the difference between AddControllers, AddControllersWithViews, AddRazorPages and AddMvc?

ASP.NET Core Fundamentals Basic 1–2 Yrs2–3 Yrs Conceptual 1 min read

Quick Answer

AddControllers() — controllers + API features only (for a Web API). AddControllersWithViews() — adds Razor views and view-related features (MVC web app). AddRazorPages() — adds Razor Pages. AddMvc() — everything (controllers, views, Razor Pages, tag helpers). Pick the smallest one you need.

They register different slices of MVC. AddControllers is just controllers and the API pipeline — that's what I use for a pure Web API, and it skips loading the whole view engine. AddControllersWithViews adds Razor views for a server-rendered MVC app. AddRazorPages adds Razor Pages. AddMvc turns on all of it. They're additive and each returns a builder you can chain options onto, like JSON serializer settings.

  • API project → AddControllers() — no view engine loaded, smaller footprint.
  • Server-rendered app → AddControllersWithViews() and/or AddRazorPages().
  • They're additive: you can call more than one.
  • All return an IMvcBuilder you can chain (.AddJsonOptions(...), .AddFluentValidation()).