Difference between AddMvc vs AddMvcCore

0

In some examples I've been tracking, I've found two calls in Startup of applications in asp.net-core

AddMvc:

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

AddMvcCore:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
}
  • What would your differences be?
  • When to use one or the other?
asked by anonymous 22.04.2018 / 19:29

1 answer

2

I've started messing with asp.net-core and I was curious about the question. Looking at the doc and the source code, we have the two calls with the following code:

TL; DR: The AddMvc implements more items than the AddMvcCore , which in turn implements only the "basic".

AddMvc:

public static IMvcBuilder AddMvc(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    var builder = services.AddMvcCore();

    builder.AddApiExplorer();
    builder.AddAuthorization();

    AddDefaultFrameworkParts(builder.PartManager);

    builder.AddFormatterMappings();
    builder.AddViews();
    builder.AddRazorViewEngine();
    builder.AddCacheTagHelper();

    builder.AddDataAnnotations(); // +1 order

    builder.AddJsonFormatters();

    builder.AddCors();

    return new MvcBuilder(builder.Services, builder.PartManager);
}

AddMvcCore

public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    var partManager = GetApplicationPartManager(services);
    services.TryAddSingleton(partManager);

    ConfigureDefaultFeatureProviders(partManager);
    ConfigureDefaultServices(services);
    AddMvcCoreServices(services);

    var builder = new MvcCoreBuilder(services, partManager);

    return builder;
}
  • % is a "greater" method, because in addition to calling AddMvc , it calls "core" , AddJsonFormatters() , AddCacheTagHelper() , AddRazorViewEngine() and AddViews() . >
  • The AddFormatterMappings() is much shorter because it calls only the basic items.

When you use AddMvcCore all items like, formatting AddMvcCore , json , razor , and so on, must be added "on hand". One way to do this would be cache .

About when to use one and when not to use another. (based on opinion) It all depends on which application is being developed, there is a need in the "root" of the system to have implemented items such as services.AddMvcCore().AddJsonFormatters() , razor or is it an application that will not use these items?

obs: maybe this answer about when to use does not please

    
22.04.2018 / 20:25