MvcServiceCollectionExtensions.AddControllersWithViews Method

Definition

Overloads

AddControllersWithViews(IServiceCollection)

Adds services for controllers to the specified IServiceCollection. This method will not register services used for pages.

AddControllersWithViews(IServiceCollection, Action<MvcOptions>)

Adds services for controllers to the specified IServiceCollection. This method will not register services used for pages.

AddControllersWithViews(IServiceCollection)

Source:
MvcServiceCollectionExtensions.cs

Adds services for controllers to the specified IServiceCollection. This method will not register services used for pages.

C#
public static Microsoft.Extensions.DependencyInjection.IMvcBuilder AddControllersWithViews (this Microsoft.Extensions.DependencyInjection.IServiceCollection services);

Parameters

services
IServiceCollection

The IServiceCollection to add services to.

Returns

An IMvcBuilder that can be used to further configure the MVC services.

Remarks

This method configures the MVC services for the commonly used features with controllers with views. This combines the effects of AddMvcCore(IServiceCollection), AddApiExplorer(IMvcCoreBuilder), AddAuthorization(IMvcCoreBuilder), AddCors(IMvcCoreBuilder), AddDataAnnotations(IMvcCoreBuilder), AddFormatterMappings(IMvcCoreBuilder), AddCacheTagHelper(IMvcCoreBuilder), AddViews(IMvcCoreBuilder), and AddRazorViewEngine(IMvcCoreBuilder).

To add services for pages call AddRazorPages(IServiceCollection).

Applies to

ASP.NET Core 9.0 and other versions
Product Versions
ASP.NET Core 3.0, 3.1, 5.0, 6.0, 7.0, 8.0, 9.0

AddControllersWithViews(IServiceCollection, Action<MvcOptions>)

Source:
MvcServiceCollectionExtensions.cs

Adds services for controllers to the specified IServiceCollection. This method will not register services used for pages.

C#
public static Microsoft.Extensions.DependencyInjection.IMvcBuilder AddControllersWithViews (this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Action<Microsoft.AspNetCore.Mvc.MvcOptions> configure);
C#
public static Microsoft.Extensions.DependencyInjection.IMvcBuilder AddControllersWithViews (this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Action<Microsoft.AspNetCore.Mvc.MvcOptions>? configure);

Parameters

services
IServiceCollection

The IServiceCollection to add services to.

configure
Action<MvcOptions>

An Action<T> to configure the provided MvcOptions.

Returns

An IMvcBuilder that can be used to further configure the MVC services.

Remarks

This method configures the MVC services for the commonly used features with controllers with views. This combines the effects of AddMvcCore(IServiceCollection), AddApiExplorer(IMvcCoreBuilder), AddAuthorization(IMvcCoreBuilder), AddCors(IMvcCoreBuilder), AddDataAnnotations(IMvcCoreBuilder), AddFormatterMappings(IMvcCoreBuilder), AddCacheTagHelper(IMvcCoreBuilder), AddViews(IMvcCoreBuilder), and AddRazorViewEngine(IMvcCoreBuilder).

To add services for pages call AddRazorPages(IServiceCollection).

Applies to

ASP.NET Core 9.0 and other versions
Product Versions
ASP.NET Core 3.0, 3.1, 5.0, 6.0, 7.0, 8.0, 9.0