From 56cb8cec1d43dd4af4738bbfc9326f7ee3c38360 Mon Sep 17 00:00:00 2001 From: John Surina <46826576+JohnSurina@users.noreply.github.com> Date: Tue, 23 Nov 2021 05:35:35 -0500 Subject: [PATCH] Update to ASP.net Configuration page (#23369) Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> --- .../fundamentals/configuration/index.md | 10 ++++++++- .../index/samples/6.x/ConfigSample/Service.cs | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Service.cs diff --git a/aspnetcore/fundamentals/configuration/index.md b/aspnetcore/fundamentals/configuration/index.md index 92957f28f7..43ec5378d0 100644 --- a/aspnetcore/fundamentals/configuration/index.md +++ b/aspnetcore/fundamentals/configuration/index.md @@ -774,6 +774,14 @@ The following code shows how to use the custom `EFConfigurationProvider` in *Pro [!code-csharp[](index/samples_snippets/6.x/EfconfigSample/Program.cs?highlight=5-6)] +## Access configuration with Dependency Injection (DI) + +Configuration can be injected into services using [Dependency Injection (DI)](xref:fundamentals/dependency-injection) by resolving the service: + +[!code-csharp[](index/samples/6.x/ConfigSample/Service.cs?name=snippet_Class&highlight=5-6)] + +For information on how to access values using `IConfiguration`, see [GetValue](#getvalue) and [GetSection, GetChildren, and Exists](#getsection-getchildren-and-exists) in this article. + ## Access configuration in Razor Pages The following code displays configuration data in a Razor Page: @@ -1451,7 +1459,7 @@ In the preceding environment variable, `Https` is the name of the Kestrel specif ## GetValue -[`ConfigurationBinder.GetValue`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue*) extracts a single value from configuration with a specified key and converts it to the specified type: +[`ConfigurationBinder.GetValue`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue*) extracts a single value from configuration with a specified key and converts it to the specified type. This method is an extension method for : [!code-csharp[](index/samples/3.x/ConfigSample/Pages/TestNum.cshtml.cs?name=snippet)] diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Service.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Service.cs new file mode 100644 index 0000000000..9ade0d41b0 --- /dev/null +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Service.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Configuration; + +namespace ConfigSample +{ + #region snippet_Class + public class Service + { + private readonly IConfiguration _config; + + public Service(IConfiguration config) => + _config = config; + + public void DoSomething() + { + var configSettingValue = _config["ConfigSetting"]; + + // ... + } + } + #endregion +} \ No newline at end of file