【ASP.NET Core】AddMvc和AddMvcCore的區別


AddMvcCore() method only adds the core MVC services.

AddMvc() method adds all the required MVC services.

AddMvc() method calls AddMvcCore() method internally.

  

  AddMvcCore()更加簡潔,只添加了MVC核心服務。

  而AddMvc()添加了MVC所需要的所有組件。

  AddMvc()內部調用了AddMvcCore(),這也就使得AddMvcCore()更加重要。

  ASP.Net是開源項目,>github地址

  貼出AddMvc源碼會更加清楚。

  

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

            var builder = services.AddMvcCore(); // 從這里開始,都是以AddMvcCore()為基礎進行添加的各種服務。

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

            AddDefaultFrameworkParts(builder.PartManager);

            // Order added affects options setup order

            // Default framework order
            builder.AddFormatterMappings();
            builder.AddViews();
            builder.AddRazorViewEngine();
            builder.AddRazorPages();
            builder.AddCacheTagHelper();

            // +1 order
            builder.AddDataAnnotations(); // +1 order

            // +10 order
            builder.AddJsonFormatters();

            builder.AddCors();

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

 

  AddMvcCore源碼:

  

 1 public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)
 2         {
 3             if (services == null)
 4             {
 5                 throw new ArgumentNullException(nameof(services));
 6             }
 7 
 8             var partManager = GetApplicationPartManager(services);
 9             services.TryAddSingleton(partManager);
10 
11             ConfigureDefaultFeatureProviders(partManager);
12             ConfigureDefaultServices(services);
13             AddMvcCoreServices(services);
14 
15             var builder = new MvcCoreBuilder(services, partManager);
16 
17             return builder;
18         }

 

轉自油管>ASP NET Core AddMvc vs AddMvcCore


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM