Configuration namespaces and line lengths (#12526)
parent
403a41fcff
commit
bbb75be6fd
|
@ -5,7 +5,7 @@ description: Learn how to use the Configuration API to configure an ASP.NET Core
|
|||
monikerRange: '>= aspnetcore-2.1'
|
||||
ms.author: riande
|
||||
ms.custom: mvc
|
||||
ms.date: 03/11/2019
|
||||
ms.date: 05/24/2019
|
||||
uid: fundamentals/configuration/index
|
||||
---
|
||||
# Configuration in ASP.NET Core
|
||||
|
@ -22,12 +22,16 @@ App configuration in ASP.NET Core is based on key-value pairs established by *co
|
|||
* In-memory .NET objects
|
||||
* Settings files
|
||||
|
||||
Configuration packages for common configuration provider scenarios are included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app). Code examples that follow and in the sample app use the <xref:Microsoft.Extensions.Configuration> namespace:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Extensions.Configuration;
|
||||
```
|
||||
|
||||
The *options pattern* is an extension of the configuration concepts described in this topic. Options uses classes to represent groups of related settings. For more information on using the options pattern, see <xref:fundamentals/configuration/options>.
|
||||
|
||||
[View or download sample code](https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/configuration/index/samples) ([how to download](xref:index#how-to-download-a-sample))
|
||||
|
||||
These three packages are included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app).
|
||||
|
||||
## Host vs. app configuration
|
||||
|
||||
Before the app is configured and started, a *host* is configured and launched. The host is responsible for app startup and lifetime management. Both the app and the host are configured using the configuration providers described in this topic. Host configuration key-value pairs become part of the app's global configuration. For more information on how the configuration providers are used when the host is built and how configuration sources affect host configuration, see [The host](xref:fundamentals/index#host).
|
||||
|
@ -97,8 +101,6 @@ Configuration providers that implement change detection have the ability to relo
|
|||
<xref:Microsoft.Extensions.Configuration.IConfiguration> is available in the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) container. <xref:Microsoft.Extensions.Configuration.IConfiguration> can be injected into a Razor Pages <xref:Microsoft.AspNetCore.Mvc.RazorPages.PageModel> to obtain configuration for the class:
|
||||
|
||||
```csharp
|
||||
// using Microsoft.Extensions.Configuration;
|
||||
|
||||
public class IndexModel : PageModel
|
||||
{
|
||||
private readonly IConfiguration _config;
|
||||
|
@ -163,7 +165,7 @@ This sequence of providers is put into place when you initialize a new <xref:Mic
|
|||
|
||||
Call <xref:Microsoft.Extensions.Hosting.HostBuilder.ConfigureAppConfiguration*> when building the host to specify the app's configuration providers in addition to those added automatically by <xref:Microsoft.AspNetCore.WebHost.CreateDefaultBuilder*>:
|
||||
|
||||
[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=19)]
|
||||
[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=20)]
|
||||
|
||||
Configuration supplied to the app in <xref:Microsoft.Extensions.Hosting.HostBuilder.ConfigureAppConfiguration*> is available during the app's startup, including `Startup.ConfigureServices`. For more information, see the [Access configuration during startup](#access-configuration-during-startup) section.
|
||||
|
||||
|
@ -351,8 +353,9 @@ public class Program
|
|||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
// Call additional providers here as needed.
|
||||
// Call AddEnvironmentVariables last if you need to allow environment
|
||||
// variables to override values from other providers.
|
||||
// Call AddEnvironmentVariables last if you need to allow
|
||||
// environment variables to override values from other
|
||||
// providers.
|
||||
config.AddEnvironmentVariables(prefix: "PREFIX_");
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
|
@ -470,7 +473,8 @@ public class Program
|
|||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
config.AddIniFile("config.ini", optional: true, reloadOnChange: true);
|
||||
config.AddIniFile(
|
||||
"config.ini", optional: true, reloadOnChange: true);
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
|
@ -561,7 +565,8 @@ public class Program
|
|||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
config.AddJsonFile("config.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile(
|
||||
"config.json", optional: true, reloadOnChange: true);
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
|
@ -628,7 +633,8 @@ public class Program
|
|||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
config.AddXmlFile("config.xml", optional: true, reloadOnChange: true);
|
||||
config.AddXmlFile(
|
||||
"config.xml", optional: true, reloadOnChange: true);
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
|
@ -743,7 +749,8 @@ public class Program
|
|||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
var path = Path.Combine(Directory.GetCurrentDirectory(), "path/to/files");
|
||||
var path = Path.Combine(
|
||||
Directory.GetCurrentDirectory(), "path/to/files");
|
||||
config.AddKeyPerFile(directoryPath: path, optional: true);
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
|
@ -831,8 +838,6 @@ The following example:
|
|||
* Stores the value in the `NumberConfig` property for use by the page.
|
||||
|
||||
```csharp
|
||||
// using Microsoft.Extensions.Configuration;
|
||||
|
||||
public class IndexModel : PageModel
|
||||
{
|
||||
public IndexModel(IConfiguration config)
|
||||
|
@ -1012,7 +1017,7 @@ Consider the configuration keys and values shown in the following table.
|
|||
|
||||
These keys and values are loaded in the sample app using the Memory Configuration Provider:
|
||||
|
||||
[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=3-10,22)]
|
||||
[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=5-12,23)]
|
||||
|
||||
The array skips a value for index #3. The configuration binder isn't capable of binding null values or creating null entries in bound objects, which becomes clear in a moment when the result of binding this array to an object is demonstrated.
|
||||
|
||||
|
@ -1147,7 +1152,7 @@ An `AddEFConfiguration` extension method permits adding the configuration source
|
|||
|
||||
The following code shows how to use the custom `EFConfigurationProvider` in *Program.cs*:
|
||||
|
||||
[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=26)]
|
||||
[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=30-31)]
|
||||
|
||||
## Access configuration during startup
|
||||
|
||||
|
|
|
@ -11,7 +11,8 @@ namespace ConfigurationSample
|
|||
#region snippet_Program
|
||||
public class Program
|
||||
{
|
||||
public static Dictionary<string, string> arrayDict = new Dictionary<string, string>
|
||||
public static Dictionary<string, string> arrayDict =
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
{"array:entries:0", "value0"},
|
||||
{"array:entries:1", "value1"},
|
||||
|
@ -31,10 +32,14 @@ namespace ConfigurationSample
|
|||
{
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
config.AddInMemoryCollection(arrayDict);
|
||||
config.AddJsonFile("json_array.json", optional: false, reloadOnChange: false);
|
||||
config.AddJsonFile("starship.json", optional: false, reloadOnChange: false);
|
||||
config.AddXmlFile("tvshow.xml", optional: false, reloadOnChange: false);
|
||||
config.AddEFConfiguration(options => options.UseInMemoryDatabase("InMemoryDb"));
|
||||
config.AddJsonFile(
|
||||
"json_array.json", optional: false, reloadOnChange: false);
|
||||
config.AddJsonFile(
|
||||
"starship.json", optional: false, reloadOnChange: false);
|
||||
config.AddXmlFile(
|
||||
"tvshow.xml", optional: false, reloadOnChange: false);
|
||||
config.AddEFConfiguration(
|
||||
options => options.UseInMemoryDatabase("InMemoryDb"));
|
||||
config.AddCommandLine(args);
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
|
|
Loading…
Reference in New Issue