欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【ASP.NET Core】AddMvc和AddMvcCore的区别

程序员文章站 2023-10-19 11:19:05
AddMvcCore() method only adds the core MVC services. AddMvc() method adds all the required MVC services. AddMvc() method calls AddMvcCore() method int ......

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