AddMvc And AddMvcCore Method In ASP.NET Core MVC

Introduction

In this article you will learn the differences between AddMVC() method and AddMvcCore() in ASP.NET Core MVC. ASP.NET Core is a modular complete rewrite of the .NET cross-platform framework. This is a new framework with many awesome features. One of my favorite features is Dependency Injection. Every thing is on demand which means you have to enable whatever you need. There are a couple of methods to configure the MVC framework. Let's discuss each in detail.

AddMvcCore Method

Adding this method enables the minimum dependency required to run the MVC framework. It adds essential MVC services to the specified method.

What is in this method?

  • ApplicationPartManager
    Manages the parts and features(list of controllers) of an MVC application.

  • DefaultFeatureProviders 
    Adds the controller feature providers

  • Default services 
    Adds the routing

  • CoreServiecs 
    These are the basic services to run the MVC framework with minimum dependencies.
  1. /// <summary>  
  2.         /// Adds essential MVC services to the specified <see cref="IServiceCollection" />.  
  3.         /// </summary>  
  4.         /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>  
  5.         /// <returns>An <see cref="IMvcCoreBuilder"/> that can be used to further configure the MVC services.</returns>  
  6.         public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)  
  7.         {  
  8.             if (services == null)  
  9.             {  
  10.                 throw new ArgumentNullException(nameof(services));  
  11.             }  
  12.   
  13.             var partManager = GetApplicationPartManager(services);  
  14.             services.TryAddSingleton(partManager);  
  15.   
  16.             ConfigureDefaultFeatureProviders(partManager);  
  17.             ConfigureDefaultServices(services);  
  18.             AddMvcCoreServices(services);  
  19.   
  20.             var builder = new MvcCoreBuilder(services, partManager);  
  21.   
  22.             return builder;  
  23.         }  

Let's try this method and see. I have created my MVC application. Now, I'm going to add this method in configure services in start up class.

  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2.         public void ConfigureServices(IServiceCollection services)  
  3.         {  
  4.             // Add framework services.  
  5.             services.AddMvcCore();  
  6.         }  

Now, I have created two action methods on Home Controller to see the things in action.

  1. public class HomeController : Controller  
  2.     {  
  3.         public IActionResult AddMvcCore()  
  4.         {  
  5.             return Content("AddMvcCore method called");  
  6.         }  
  7.   
  8.         public IActionResult AddMvc()  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  

Now, I'm going to browse both the actions in the browser to see the magic.

First, we are browsing the AddMvcCore action and you are able to see the content returned from Controller action.

http://localhost:1105/home/addmvccore


Now, browse the Addmvc action and you will get an error page. If you want to use MVC features, then you have to use the AddMvc method instead of AddMvcCore.

http://localhost:1105/home/addmvc

AddMvc Method 

In order to use MVC framework, you have to enable or you have to take the MVC project template . This method adds all dependencies in the project.

What is in this Method?

This is the complete dependency for MVC framework to work all the features,

  • MvcCore - This is the core dependency of MVC framework . These are the minimum dependencies required to run the MVC framework. The list of below features will not be available in this method.
  • ApiExplorer - This enables the API help pages
  • Authorization - Middleware for security and authorization of web apps.
  • DefaultFrameworkParts - This adds some application part dependencies. Application part manager takes care of application parts and application features.
  • FormatterMappings - A filter that will use the format value in the route data or query string to set the content type on an Object Result.
  • Views - This enables the Views feature and makes it work.
  • RazorViewEngine - Parser and code generator for CSHTML files are used in View pages for MVC web apps.
  • RazorPages - Razor Pages is a new feature of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive.
  • CacheTagHelper - Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files
  • DataAnnotations - Data annotations enable validations and decorates the model.
  • JsonFormatters - This enables the JSON data formatters .
  • Cors - This enables the Cross-origin resource sharing (CORS), which is a mechanism that allows restricted resources .
  1. /// <summary>  
  2.         /// Adds MVC services to the specified <see cref="IServiceCollection" />.  
  3.         /// </summary>  
  4.         /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>  
  5.         /// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>  
  6.         public static IMvcBuilder AddMvc(this IServiceCollection services)  
  7.         {  
  8.             if (services == null)  
  9.             {  
  10.                 throw new ArgumentNullException(nameof(services));  
  11.             }  
  12.   
  13.             var builder = services.AddMvcCore();  
  14.   
  15.             builder.AddApiExplorer();  
  16.             builder.AddAuthorization();  
  17.   
  18.             AddDefaultFrameworkParts(builder.PartManager);  
  19.   
  20.             // Order added affects options setup order  
  21.   
  22.             // Default framework order  
  23.             builder.AddFormatterMappings();  
  24.             builder.AddViews();  
  25.             builder.AddRazorViewEngine();  
  26.             builder.AddRazorPages();  
  27.             builder.AddCacheTagHelper();  
  28.   
  29.             // +1 order  
  30.             builder.AddDataAnnotations(); // +1 order  
  31.   
  32.             // +10 order  
  33.             builder.AddJsonFormatters();  
  34.   
  35.             builder.AddCors();  
  36.   
  37.             return new MvcBuilder(builder.Services, builder.PartManager);  
  38.         }  

Summary

In this article, we have learned the differences between both methods. This will help you to understand the internal picture of MVC frameworkand how it works.