What is the difference between AddControllers, AddControllersWithViews, AddRazorPages and AddMvc?
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/orAddRazorPages(). - They're additive: you can call more than one.
- All return an
IMvcBuilderyou can chain (.AddJsonOptions(...),.AddFluentValidation()).