AddMVC() vs AddControllersWithViews() vs AddControllers() vs AddRazorPages() in Asp.Net Core
Asp.net core has a couple of methods (AddControllers, AddMvc, AddRazorPages, AddControllersWithViews)
to design your web application as per the need without adding unnecessary services.
Lets understand the difference among AddControllers, AddMvc, AddRazorPages & AddControllersWithViews methods.
Feature name | AddMVC | AddControllers WithViews | AddControllers | Add RazorPages |
Controllers | Yes | Yes | Yes | Yes |
Model Binding | Yes | Yes | Yes | Yes |
API Explorer | Yes | Yes | Yes | No |
Authorization | Yes | Yes | Yes | Yes |
CORS | Yes | Yes | Yes | No |
Validations | Yes | Yes | Yes | Yes |
Formatter Mapping | Yes | Yes | Yes | No |
Antiforgery | Yes | Yes | No | Yes |
Temp Data | Yes | Yes | No | Yes |
Views | Yes | Yes | No | Yes |
Pages | Yes | No | No | Yes |
Tag helpers | Yes | Yes | No | Yes |
Memory cache | Yes | Yes | No | Yes |
Which method should we use in our application?
- Web API – AddControllers()
- Razor pages – AddRazorPage()
- MVC – AddControllersWithViews()
- Else – AddMVC()