ASP.NET Core 中 AddMvc() 與 AddMvcCore() 的差別

ASP.NET Core 將過去 ASP.NET MVC 與 ASP.NET Web API 兩套 framework 整合在一起,對於開發人員是種福音:不用再想到底該引用哪個 NameSpace、不用再為該繼承哪個類別煩惱、不用再記兩種開發模式(到底該用 action filter 還是 MessageHandler),不過實際上在 ASP.NET Core 中還是有 AddMvc() 與 AddMvcCore() 兩種服務註冊方式

其中差別剛看到時實在沒什麼想法,幸虧 ASP.NET Core 程式碼都是 open source,覺得有疑問馬上可以翻一下,今天就簡單紀錄一下自己看到的差異

基本環境說明

  1. macOS Mojave 10.14.5
  2. .NET Core SDK 2.2.107 (.NET Core Runtime 2.2.5)

AddMvcCore

完整程式碼請參考 AddMvcCore

程式碼節錄如下:

public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }
    var partManager = GetApplicationPartManager(services);
    services.TryAddSingleton(partManager);
    ConfigureDefaultFeatureProviders(partManager);
    ConfigureDefaultServices(services);
    AddMvcCoreServices(services);
    var builder = new MvcCoreBuilder(services, partManager);
    return builder;
}

AddMvc

完整程式碼請參考 AddMvc

程式碼節錄如下:

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

public static IMvcBuilder AddControllersWithViews(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }
    var builder = AddControllersWithViewsCore(services);
    return new MvcBuilder(builder.Services, builder.PartManager);
}

private static IMvcCoreBuilder AddControllersWithViewsCore(IServiceCollection services)
{
    var builder = AddControllersCore(services)
        .AddViews()
        .AddRazorViewEngine()
        .AddCacheTagHelper();
    AddTagHelpersFrameworkParts(builder.PartManager);
    return builder;
}
....

private static IMvcCoreBuilder AddControllersCore(IServiceCollection services)
{
    // This method excludes all of the view-related services by default.
    return services
        .AddMvcCore()
        .AddApiExplorer()
        .AddAuthorization()
        .AddCors()
        .AddDataAnnotations()
        .AddFormatterMappings();
}

trace code

從 AddMvc 追起就可以發現最後殊途同歸:最後還是使用到 AddMvcCore

services.AddControllersWithViews();

–> public static IMvcBuilder AddControllersWithViews(this IServiceCollection services)

–> var builder = AddControllersWithViewsCore(services);

–> private static IMvcCoreBuilder AddControllersWithViewsCore(IServiceCollection services)

–> var builder = AddControllersCore(services)

–> private static IMvcCoreBuilder AddControllersCore(IServiceCollection services)

–> .AddMvcCore()

心得

以程式碼來看 AddMvc 比 AddMvcCore 多了下列內容

  1. AddRazorPages
  2. AddViews
  3. AddRazorViewEngine
  4. AddCacheTagHelper
  5. AddApiExplorer
  6. AddAuthorization
  7. AddCors
  8. AddDataAnnotations
  9. AddFormatterMappings
  10. AddJsonFormatters

大家在使用時可以評估看看用哪個方法比較合適

參考資訊

  1. ASP.NET Core AddMvc() Vs AddMvcCore()