Benutzer:MovGP0/ASP.NET Core/Configuration
aus Wikipedia, der freien Enzyklopädie
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
ASP.NET Core ConfigurationLoad Settings
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) // later file overwrites the previous
.AddEnvironmentVariables();
Configuration = builder.Build();
Inject Settings
{
"MySettings": {
"SomeSetting": "Test2",
"Another": true
}
}
services.AddSingleton<IConfiguration>(Configuration);
public HomeController(IConfiguration config)
{
string setting = config["MySettings:SomeSetting"];
bool another = bool.Parse(config["MySettings:Another"]);
}
Options
{
"MySettings": {
"SomeSetting": "Test2",
"Another": true
}
}
public class MySettings
{
public string SomeSetting { get; set; }
public bool Another { get; set; }
}
services.AddOptions();
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
public HomeController(IOptions<MySettings> config)
{
string setting = config.Value.SomeSetting;
bool another = config.Value.Another;
}
Configuration SourcesXMLNuGet:
<configuration>
<MySettings>
<SomeSetting>Test</SomeSetting>
<Another>true</Another>
</MySettings>
</configuration>
app.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)
ININuGet:
[MySettings]
SomeSetting=TestIni
Another=true
.AddIniFile("appsettings.ini", optional: true, reloadOnChange: true)
Environment VariablesNuGet:
$env:'MySettings:SomeSetting' = "Setting value"
$env:'MySettings__Another' = "true"
$env:'MySettings:MoreSettings:ThirdSetting' = "Some value"
app.AddEnvironmentVariables()
Command-Line ArgumentsNuGet:
. ./MyWebApp.exe --mysettings:somesetting "Some value from commandline"
app.AddCommandLine(Environment.GetCommandLineArgs().Skip(1).ToArray())
In-Memory
app.AddInMemoryCollection(new Dictionary<string, string>
{
{"MySettings:SomeSetting", "MyValue" }
})
User SecretsNuGet:
// use project.json for ASP.NET Core v1.0.0
[assembly: UserSecretsId("aspnet-TestApp-20170103063931")]
builder.AddUserSecrets<Startup>();
Quellen
|