AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages

The ASP.NET Core MVC Services in 3.0 & later versions provides four extension methods to add MVC Services. They are AddMvc, AddController, AddControllersWithViews & AddRazorPages.

AddMvcCore

AddMVCCore method registers only a bare minimum services that we need to start an MVC Application. It Registers the lower level services like Controller Factory, which creates the controllers, services that selects the Action method & services that invoke the Action method. It also includes model binding services etc.

We can create three different types of web applications (or any combination of them) using the MVC Services. They are

  1. MVC (Controller with View)
  2. Web API
  3. Razor Pages

The above application types require additional services to work. This is where the methods like AddMvc, AddController, AddControllersWithViews & AddRazorPages comes into picture.

AddMvc Vs AddController Vs AddControllersWithViews Vs AddRazorPages

The above methods first invoke the AddMvcCore method and then go onto registers the additional services. The following table shows list of services, that each of the above methods registers

These extension methods defined in the MvcServiceCollectionExtensions namespace. You can also check the Source Code to see what these methods does.

OptionsAddControllerAddControllersWithViewsAddRazorPagesAddMvc
MVC Core Services
AddMvcCore()

Controller Creation & Activation, Model Binding
YesYesYesYes
API ExplorerYesYesNoYes
AuthorizationYesYesYesYes
CORSYesYesNoYes
Data AnnotationsYesYesYesYes
Formatter MappingYesYesNoYes
Support for Views
(View Engine and related infrastructure, HTMLHelper, View Components, Tempdata, AntiForgery, Component rendering, etc )
NoYesYesYes
Razor View Engine
(TagHelpers etc)
NoYesYesYes
CacheTagHelperNoYesYesYes
Support for Razor Pages
NoNoYesYes

AddController

The AddController method Registers everything that is needed for Web API Development. The services include Support for Controllers, Model Binding, API Explorer, Authorization, CORS, Validations, Formatter Mapping, etc.

AddControllersWithViews

The AddControllersWithViews method registers everything that is needed for Web App Development using Controllers and Views. It registers everything that AddController installs plus the support for Views. These include View Engine and related infrastructure, HTMLHelper, View Components, Tempdata, AntiForgery, Component rendering, etc

AddRazorPages

The AddRazorPages method registers everything everything needed for Web app development using the Razor Pages. It installs all the services except API Explorer, CORS & Formatter Mapping. It also installs the support for the support for Views, which includes View Engine and related infrastructure, HTMLHelper, View Components, Tempdata, AntiForgery, Component rendering, etc

AddMvc

The AddMvc method installs all the services.

References

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top