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
.
Table of Contents
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
- MVC (Controller with View)
- Web API
- 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.
Options | AddController | AddControllersWithViews | AddRazorPages | AddMvc |
---|---|---|---|---|
MVC Core Services AddMvcCore() Controller Creation & Activation, Model Binding | Yes | Yes | Yes | Yes |
API Explorer | Yes | Yes | No | Yes |
Authorization | Yes | Yes | Yes | Yes |
CORS | Yes | Yes | No | Yes |
Data Annotations | Yes | Yes | Yes | Yes |
Formatter Mapping | Yes | Yes | No | Yes |
Support for Views (View Engine and related infrastructure, HTMLHelper, View Components, Tempdata, AntiForgery, Component rendering, etc ) | No | Yes | Yes | Yes |
Razor View Engine (TagHelpers etc) | No | Yes | Yes | Yes |
CacheTagHelper | No | Yes | Yes | Yes |
Support for Razor Pages | No | No | Yes | Yes |
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.