ASP.NET CORE AddMVC Vs AddMVCCore (Bootrapping ways)

In this tutorial we will discuss the difference between AddMvc() and AddMvcCore() vs AddControllersWithViews() vs AddControllers() vs AddRazorPages() While working on empty asp.net core web application template the first question comes to mind is Which method should we use to configure our application in ConfigureServices method of startup class

There are several ways of bootstrapping your MVC applications on top of ASP.NET Core 3.x.  Depending on your requirement is your choice to choose the correct one such as  controllers, views, pages and expose them as HTTP endpoints.

In .NET Core 2.x and earlier, you could register the MVC framework in the ASP.NET Core dependency injection container in two ways:

      1. services.AddMvc()
      2. services.AddMvcCore()
In ASP.NET Core 3.x and above version added another three additional ways:
 
      1. services.AddControllers()
      2. services.AddControllersWithViews()
      3. services.AddRazorPages()

AddMvcCore()

The AddMvcCore() method adds only the required MVC services to the dependency injection container whereas the AddMvc() method adds all the required MVC services. As you can see from the source code of the AddMvc() method, it internally calls the AddMvcCore() method to add all the core services which are required for MVC. As we ASP.Net Core open sources you can check below code from official git code.
 
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
 

AddControllers()

Adds services for controllers to the specified IServiceCollection. This method will not register services used for views or pages. This method configures the MVC services for the commonly used features with controllers for an API.
 
This combines the effects of AddMvcCore(IServiceCollection), AddApiExplorer(IMvcCoreBuilder), AddAuthorization(IMvcCoreBuilder), AddCors(IMvcCoreBuilder), AddDataAnnotations(IMvcCoreBuilder), and AddFormatterMappings(IMvcCoreBuilder).
 
In other words, what you can expect from AddControllers() is that it would give you the most comfortable setup for API development.
 
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
AddControllersWithViews()
 
This is the one you should pick if you are building a “classic” MVC site, just like we have been doing it for years – with controllers and Razor views. It will end up activating: This should be the default choice for you if you do not need the new Razor pages functionality – you are either building the MVC website exactly how it was built in old desktop framework MVC and in earlier versions of ASP.NET Core MVC or if you are migrating an older site
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
       
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

AddRazorPages()

Razor Pages is a new aspect of ASP.NET Core MVC introduced in ASP.NET Core 3.x. It offers a “page-based” approach for building server-side rendered apps in ASP.NET Core and can coexist with “traditional” MVC or Web API controllers. In this post I provide an introduction to Razor Pages, the basics of getting started, and how Razor Pages differs from MVC.
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
}

AddMvc()

This simply registers the entire above methods in dependency injection. AddMvc() method adds all the required MVC services. As you can see from the source code of the AddMvc() method, it internally calls the AddMvcCore() method to add all the core services which are required for MVC.
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

You can watch our video version of this tutorial with step by step explanation.