加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 09:18:35 所属栏目:asp.Net 来源:网络整理
导读: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所需要的所有组件

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读