比较一下AddMvc、AddMvcCore、AddController等在服务配置时的区别及各自拥有的功能。

1. services.AddMvcCore()

只注册路由请求和执行控制器所必要的核心服务,确保 Pipeline 程序可运转。除非是有能力并想完全去自主DIY,一般不建议直接使用这个。

2. services.AddControllers()

除包含了 AddMvcCore() 所有功能,再加上:
  • Authorization
  • ApiExplorer
  • Data Annotation
  • Formatter Mapping
  • CORS
要用 Controller 但不用View,新建WebAPI时,默认采用的就是这个,使用这个时,与SwashBuckle配合时,无需再额外引入ApiExplorer,自身已经依赖。

3. services.AddRazorPages()

包含 AddMvcCore() 所有功能,再加上:
  • Razor Pages
  • Authorization
  • Data Annotation
  • Cache Tag Helper

4. services.AddControllersWithViews()

包含 AddControllers() 所有功能,再加上:
  • cshtml和Razor View
  • Cache Tag Helper
标准MVC模式,常用Razor视图,使用这个就够了

5. services.AddMvc()

包含 AddControllersWithViews() 及 AddRazorPages() 功能。 包含的功能最为齐全,如果不想遗漏功能,直接使用这个就行。 一张图应该能准确的概括上面一堆文字叙述:

.NET Core | AddMvc、AddMvcCore、AddControllers - 图1

6. 差不多明白了区别,看一看源码吧:

MvcServiceCollectionExtensions.AddMvc()

  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. services.AddControllersWithViews();
  13. return services.AddRazorPages();
  14. }
  15. /// <summary>
  16. /// Adds MVC services to the specified <see cref="IServiceCollection" />.
  17. /// </summary>
  18. /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
  19. /// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
  20. /// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
  21. public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
  22. {
  23. if (services == null)
  24. {
  25. throw new ArgumentNullException(nameof(services));
  26. }
  27. if (setupAction == null)
  28. {
  29. throw new ArgumentNullException(nameof(setupAction));
  30. }
  31. var builder = services.AddMvc();
  32. builder.Services.Configure(setupAction);
  33. return builder;
  34. }

MvcServiceCollectionExtensions.AddControllersWithViews()

  1. /// <summary>
  2. /// Adds services for controllers to the specified <see cref="IServiceCollection"/>. This method will not
  3. /// register services used for pages.
  4. /// </summary>
  5. /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
  6. /// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
  7. /// <remarks>
  8. /// <para>
  9. /// This method configures the MVC services for the commonly used features with controllers with views. This
  10. /// combines the effects of <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>,
  11. /// <see cref="MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(IMvcCoreBuilder)"/>,
  12. /// <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>,
  13. /// <see cref="MvcCorsMvcCoreBuilderExtensions.AddCors(IMvcCoreBuilder)"/>,
  14. /// <see cref="MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(IMvcCoreBuilder)"/>,
  15. /// <see cref="MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(IMvcCoreBuilder)"/>,
  16. /// <see cref="TagHelperServicesExtensions.AddCacheTagHelper(IMvcCoreBuilder)"/>,
  17. /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/>,
  18. /// and <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
  19. /// </para>
  20. /// <para>
  21. /// To add services for pages call <see cref="AddRazorPages(IServiceCollection)"/>.
  22. /// </para>
  23. /// </remarks>
  24. public static IMvcBuilder AddControllersWithViews(this IServiceCollection services)
  25. {
  26. if (services == null)
  27. {
  28. throw new ArgumentNullException(nameof(services));
  29. }
  30. var builder = AddControllersWithViewsCore(services);
  31. return new MvcBuilder(builder.Services, builder.PartManager);
  32. }
  33. /// <summary>
  34. /// Adds services for controllers to the specified <see cref="IServiceCollection"/>. This method will not
  35. /// register services used for pages.
  36. /// </summary>
  37. /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
  38. /// <param name="configure">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
  39. /// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
  40. /// <remarks>
  41. /// <para>
  42. /// This method configures the MVC services for the commonly used features with controllers with views. This
  43. /// combines the effects of <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>,
  44. /// <see cref="MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(IMvcCoreBuilder)"/>,
  45. /// <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>,
  46. /// <see cref="MvcCorsMvcCoreBuilderExtensions.AddCors(IMvcCoreBuilder)"/>,
  47. /// <see cref="MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(IMvcCoreBuilder)"/>,
  48. /// <see cref="MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(IMvcCoreBuilder)"/>,
  49. /// <see cref="TagHelperServicesExtensions.AddCacheTagHelper(IMvcCoreBuilder)"/>,
  50. /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/>,
  51. /// and <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
  52. /// </para>
  53. /// <para>
  54. /// To add services for pages call <see cref="AddRazorPages(IServiceCollection)"/>.
  55. /// </para>
  56. /// </remarks>
  57. public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Action<MvcOptions> configure)
  58. {
  59. if (services == null)
  60. {
  61. throw new ArgumentNullException(nameof(services));
  62. }
  63. // This method excludes all of the view-related services by default.
  64. var builder = AddControllersWithViewsCore(services);
  65. if (configure != null)
  66. {
  67. builder.AddMvcOptions(configure);
  68. }
  69. return new MvcBuilder(builder.Services, builder.PartManager);
  70. }
通过对比发现AddMvc方法比AddControllersWithViews多了一个AddRazorPages()