diff --git a/aspnetcore/mvc/controllers/dependency-injection.md b/aspnetcore/mvc/controllers/dependency-injection.md index 44339ebb7e29..4a847aa7e7e3 100644 --- a/aspnetcore/mvc/controllers/dependency-injection.md +++ b/aspnetcore/mvc/controllers/dependency-injection.md @@ -1,9 +1,10 @@ --- title: Dependency injection into controllers in ASP.NET Core +ai-usage: ai-assisted author: ardalis description: Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.NET Core. ms.author: tdykstra -ms.date: 10/13/2022 +ms.date: 03/02/2026 uid: mvc/controllers/dependency-injection --- # Dependency injection into controllers in ASP.NET Core @@ -71,6 +72,27 @@ The following code requests the `IOptions` settings from the [!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)] +## Controllers as services + +By default, ASP.NET Core doesn't register controllers as services in the DI container. The runtime uses the [DefaultControllerActivator](https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerActivator.cs) to create controller instances and resolves services from the DI container for constructor parameters, but the controller itself isn't resolved from the container. + +Calling `AddControllersAsServices` registers all controllers as services in the DI container: + +```csharp +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllersWithViews().AddControllersAsServices(); +``` + +Registering controllers as services enables: + +* Intercepting controller creation with a custom `IControllerActivator`. +* Using any DI lifetime management for controllers. +* Injecting services into controllers using any registered constructor, since the DI container selects the constructor. + +> [!NOTE] +> Configure the `ApplicationPartManager` **before** calling `AddControllersAsServices`. See for details. + ## Additional resources * See to learn how to make code easier to test by explicitly requesting dependencies in controllers. diff --git a/aspnetcore/mvc/controllers/includes/dependency-injection7.md b/aspnetcore/mvc/controllers/includes/dependency-injection7.md index 63da77b4de8c..aa20309f2943 100644 --- a/aspnetcore/mvc/controllers/includes/dependency-injection7.md +++ b/aspnetcore/mvc/controllers/includes/dependency-injection7.md @@ -55,6 +55,28 @@ The following code requests the `IOptions` settings from the [!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)] +## Controllers as services + +By default, ASP.NET Core doesn't register controllers as services in the DI container. The runtime uses the [DefaultControllerActivator](https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerActivator.cs) to create controller instances and resolves services from the DI container for constructor parameters, but the controller itself isn't resolved from the container. + +Calling `AddControllersAsServices` registers all controllers as services in the DI container: + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddControllersWithViews().AddControllersAsServices(); +} +``` + +Registering controllers as services enables: + +* Intercepting controller creation with a custom `IControllerActivator`. +* Using any DI lifetime management for controllers. +* Injecting services into controllers using any registered constructor, since the DI container selects the constructor. + +> [!NOTE] +> Configure the `ApplicationPartManager` **before** calling `AddControllersAsServices`. See for details. + ## Additional resources * See to learn how to make code easier to test by explicitly requesting dependencies in controllers. @@ -117,6 +139,28 @@ The following code requests the `IOptions` settings from the [!code-csharp[](~/mvc/controllers/dependency-injection/sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)] +## Controllers as services + +By default, ASP.NET Core doesn't register controllers as services in the DI container. The runtime uses the [DefaultControllerActivator](https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerActivator.cs) to create controller instances and resolves services from the DI container for constructor parameters, but the controller itself isn't resolved from the container. + +Calling `AddControllersAsServices` registers all controllers as services in the DI container: + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddMvc().AddControllersAsServices(); +} +``` + +Registering controllers as services enables: + +* Intercepting controller creation with a custom `IControllerActivator`. +* Using any DI lifetime management for controllers. +* Injecting services into controllers using any registered constructor, since the DI container selects the constructor. + +> [!NOTE] +> Configure the `ApplicationPartManager` **before** calling `AddControllersAsServices`. See for details. + ## Additional resources * See to learn how to make code easier to test by explicitly requesting dependencies in controllers. @@ -124,3 +168,4 @@ The following code requests the `IOptions` settings from the * [Replace the default dependency injection container with a third party implementation](xref:fundamentals/dependency-injection#default-service-container-replacement). :::moniker-end + diff --git a/aspnetcore/tutorials/razor-pages/razor-pages-start/media/net10-additional-info.png b/aspnetcore/tutorials/razor-pages/razor-pages-start/media/net10-additional-info.png index 71791ae36ade..33ada3d8c3d3 100644 Binary files a/aspnetcore/tutorials/razor-pages/razor-pages-start/media/net10-additional-info.png and b/aspnetcore/tutorials/razor-pages/razor-pages-start/media/net10-additional-info.png differ