Skip to content

Commit cee5341

Browse files
* fix * fix
1 parent 3901e58 commit cee5341

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

aspnetcore/mvc/controllers/dependency-injection.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Dependency injection into controllers in ASP.NET Core
33
author: ardalis
44
description: Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.NET Core.
55
ms.author: riande
6-
ms.date: 10/12/2022
6+
ms.date: 10/13/2022
77
uid: mvc/controllers/dependency-injection
88
---
99
# Dependency injection into controllers in ASP.NET Core
@@ -21,49 +21,49 @@ ASP.NET Core MVC controllers request dependencies explicitly via constructors. A
2121

2222
Services are added as a constructor parameter, and the runtime resolves the service from the service container. Services are typically defined using interfaces. For example, consider an app that requires the current time. The following interface exposes the `IDateTime` service:
2323

24-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Interfaces/IDateTime.cs?name=snippet)]
24+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Interfaces/IDateTime.cs?name=snippet)]
2525

2626
The following code implements the `IDateTime` interface:
2727

28-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Services/SystemDateTime.cs?name=snippet)]
28+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Services/SystemDateTime.cs?name=snippet)]
2929

3030
Add the service to the service container:
3131

32-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Startup1.cs?name=snippet&highlight=3)]
32+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Startup1.cs?name=snippet&highlight=3)]
3333

3434
For more information on <xref:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton*>, see [DI service lifetimes](xref:fundamentals/dependency-injection#service-lifetimes).
3535

3636
The following code displays a greeting to the user based on the time of day:
3737

38-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Controllers/HomeController.cs?name=snippet)]
38+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/HomeController.cs?name=snippet)]
3939

4040
Run the app and a message is displayed based on the time.
4141

4242
## Action injection with `FromServices`
4343

4444
The <xref:Microsoft.AspNetCore.Mvc.FromServicesAttribute> enables injecting a service directly into an action method without using constructor injection:
4545

46-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Controllers/HomeController.cs?name=snippet2)]
46+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/HomeController.cs?name=snippet2)]
4747

4848
## Access settings from a controller
4949

5050
Accessing app or configuration settings from within a controller is a common pattern. The *options pattern* described in <xref:fundamentals/configuration/options> is the preferred approach to manage settings. Generally, don't directly inject <xref:Microsoft.Extensions.Configuration.IConfiguration> into a controller.
5151

5252
Create a class that represents the options. For example:
5353

54-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Models/SampleWebSettings.cs?name=snippet)]
54+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Models/SampleWebSettings.cs?name=snippet)]
5555

5656
Add the configuration class to the services collection:
5757

58-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Startup.cs?highlight=4&name=snippet1)]
58+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Startup.cs?highlight=4&name=snippet1)]
5959

6060
Configure the app to read the settings from a JSON-formatted file:
6161

62-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Program.cs?name=snippet&range=10-15)]
62+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Program.cs?name=snippet&range=10-15)]
6363

6464
The following code requests the `IOptions<SampleWebSettings>` settings from the service container and uses them in the `Index` method:
6565

66-
[!code-csharp[](dependency-injection/3.1sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)]
66+
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)]
6767

6868
## Additional resources
6969

0 commit comments

Comments
 (0)