ASP.NET Core 3 Service…

ASP.NET Core 3 Service Registration – AddControllers() AddControllersWithViews() AddRazorPages()

With v3 and higher, there are several ways to register MVC. This way the application can only use what it requires, not the whole framework. This would result with a smaller size and being not sure, better performance.

AddControllers()

If you are developing an API, go with this. This is only for API-related features, Views or pages are not supported.

AddControllersWithViews();

This adds API + Views but not Pages. If you “dotnet new mvc”, it uses this code.

AddRazorPages()

This adds support for Razor Pages and minimal controller support. If you “dotnet new web”, it uses this code.

These can be combined as well. The following is equivalent to calling AddMvc in ASP.NET Core 2.2:

  
services.AddControllersWithViews();
services.AddRazorPages();

Resources:

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio-code#mvc-service-registration

https://www.strathweb.com/2020/02/asp-net-core-mvc-3-x-addmvc-addmvccore-addcontrollers-and-other-bootstrapping-approaches/

#aspnetcore #dotnet #net-core