C# services.AddControllersWithViews()与services.AddMvc()的比较

C# services.AddControllersWithViews()与services.AddMvc()的比较,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,为了能够在我的ASP.NET核心应用程序中添加控制器,我可以添加 services.AddControllersWithViews() 或 在ConfigureServices方法的Startup类中 看起来他们俩都很适合我。我想知道在什么情况下哪个更好 据我所知,services.AddMvc()是较旧的方法,但仍然可用 如果我继续使用services.AddMvc()将来会有问题吗?源代码本身就说明了问题 // ///将MVC服务添加到指定的。 /// ///要将服务添加到的。 ///

为了能够在我的ASP.NET核心应用程序中添加控制器,我可以添加

services.AddControllersWithViews()

ConfigureServices
方法的
Startup
类中

看起来他们俩都很适合我。我想知道在什么情况下哪个更好

据我所知,
services.AddMvc()
是较旧的方法,但仍然可用


如果我继续使用
services.AddMvc()
将来会有问题吗?

源代码本身就说明了问题

//
///将MVC服务添加到指定的。
/// 
///要将服务添加到的。
///可用于进一步配置MVC服务的。
公共静态IMvcBuilder AddMvc(此IServiceCollection服务)
{
if(服务==null)
{
抛出新的ArgumentNullException(nameof(services));
}
services.AddControllersWithViews();
return services.AddRazorPages();
}
/// 
///将MVC服务添加到指定的。
/// 
///要将服务添加到的。
///配置所提供的。
///可用于进一步配置MVC服务的。
公共静态IMvcBuilder AddMvc(此IServiceCollection服务,操作setupAction)
{
if(服务==null)
{
抛出新的ArgumentNullException(nameof(services));
}
if(setupAction==null)
{
抛出新ArgumentNullException(nameof(setupAction));
}
var builder=services.AddMvc();
builder.Services.Configure(setupAction);
返回生成器;
}

//
///将控制器的服务添加到指定的。这种方法是不可行的
///注册用于页面的服务。
/// 
///要将服务添加到的。
///可用于进一步配置MVC服务的。
/// 
/// 
///此方法使用带有视图的控制器为常用功能配置MVC服务。这
///结合了,
/// ,
/// ,
/// ,
/// ,
/// ,
/// ,
/// ,
///和。
/// 
/// 
///要为页面添加服务,请调用。
/// 
/// 
公共静态IMvcBuilder AddControllers with Views(此IServiceCollection services)
{
if(服务==null)
{
抛出新的ArgumentNullException(nameof(services));
}
var builder=添加具有ViewScore(服务)的控制器;
返回新的MvcBuilder(builder.Services、builder.PartManager);
}
/// 
///将控制器的服务添加到指定的。这种方法是不可行的
///注册用于页面的服务。
/// 
///要将服务添加到的。
///配置所提供的。
///可用于进一步配置MVC服务的。
/// 
/// 
///此方法使用带有视图的控制器为常用功能配置MVC服务。这
///结合了,
/// ,
/// ,
/// ,
/// ,
/// ,
/// ,
/// ,
///和。
/// 
/// 
///要为页面添加服务,请调用。
/// 
/// 
公共静态IMvcBuilder AddControllersWithView(此IServiceCollection服务,操作配置)
{
if(服务==null)
{
抛出新的ArgumentNullException(nameof(services));
}
//默认情况下,此方法排除所有与视图相关的服务。
var builder=添加具有ViewScore(服务)的控制器;
如果(配置!=null)
{
builder.addmvcopions(配置);
}
返回新的MvcBuilder(builder.Services、builder.PartManager);
}
如您所见,
AddMvc
基本上是通过添加调用
AddRazorPages
来包装对
addcontrollerswithview
的调用

如果我继续使用services.AddMvc(),将来会有问题吗

真的没有办法准确地回答这个问题


遵循当前可用文档提供的建议,避免任何不必要的行为

。我从来没有想到过。
services.AddMvc()
/// <summary>
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    services.AddControllersWithViews();
    return services.AddRazorPages();
}

/// <summary>
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    if (setupAction == null)
    {
        throw new ArgumentNullException(nameof(setupAction));
    }

    var builder = services.AddMvc();
    builder.Services.Configure(setupAction);

    return builder;
}
/// <summary>
/// Adds services for controllers to the specified <see cref="IServiceCollection"/>. This method will not
/// register services used for pages.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
/// <remarks>
/// <para>
/// This method configures the MVC services for the commonly used features with controllers with views. This
/// combines the effects of <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>,
/// <see cref="MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>,
/// <see cref="MvcCorsMvcCoreBuilderExtensions.AddCors(IMvcCoreBuilder)"/>,
/// <see cref="MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(IMvcCoreBuilder)"/>,
/// <see cref="TagHelperServicesExtensions.AddCacheTagHelper(IMvcCoreBuilder)"/>,
/// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/>,
/// and <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
/// </para>
/// <para>
/// To add services for pages call <see cref="AddRazorPages(IServiceCollection)"/>.
/// </para>
/// </remarks>
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);
}

/// <summary>
/// Adds services for controllers to the specified <see cref="IServiceCollection"/>. This method will not
/// register services used for pages.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="configure">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
/// <remarks>
/// <para>
/// This method configures the MVC services for the commonly used features with controllers with views. This
/// combines the effects of <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>,
/// <see cref="MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>,
/// <see cref="MvcCorsMvcCoreBuilderExtensions.AddCors(IMvcCoreBuilder)"/>,
/// <see cref="MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(IMvcCoreBuilder)"/>,
/// <see cref="TagHelperServicesExtensions.AddCacheTagHelper(IMvcCoreBuilder)"/>,
/// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/>,
/// and <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
/// </para>
/// <para>
/// To add services for pages call <see cref="AddRazorPages(IServiceCollection)"/>.
/// </para>
/// </remarks>
public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Action<MvcOptions> configure)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    // This method excludes all of the view-related services by default.
    var builder = AddControllersWithViewsCore(services);
    if (configure != null)
    {
        builder.AddMvcOptions(configure);
    }

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