Difference between Addmvc vs Addmvccore

Asked

Viewed 284 times

0

In some examples I’ve been following, I’ve found two calls on 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 be their differences?
  • When using one or the other?

1 answer

4


I’ve been starting to mess with asp.net-coreand 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 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;
}
  • The AddMvc is a "greater" method, because in addition to calling the "core", flame AddJsonFormatters(), AddCacheTagHelper(), AddRazorViewEngine(), AddViews() and AddFormatterMappings().

  • The AddMvcCore is much shorter because it only calls the basic items.

When the AddMvcCore all items like, format json, razor, treatment of cache etc, should be added "in the hand". A way to do this would be services.AddMvcCore().AddJsonFormatters().

About when to use one and when not to use another. (based on opinion) It all depends on which application is developing, there is a need for the "root" of the system to be implemented such items as razor, json formater or is an application that will not make use of these items?.

Bs: Maybe this answer about when to use does not please

Browser other questions tagged

You are not signed in. Login or sign up in order to post.