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;
}
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