Benutzer:MovGP0/ASP.NET Core/Configuration

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

ASP.NET Core Configuration

[Bearbeiten | Quelltext bearbeiten]
appsettings.json
{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}
Startup.cs
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

[Bearbeiten | Quelltext bearbeiten]
appsettings.json
{
  "MySettings": {
    "SomeSetting": "Test2",
    "Another": true
  }
}
Startup.cs
services.AddSingleton<IConfiguration>(Configuration);
HomeController.cs
public HomeController(IConfiguration config)
{
    string setting = config["MySettings:SomeSetting"];
    bool another = bool.Parse(config["MySettings:Another"]);
}
appsettings.json
{
  "MySettings": {
    "SomeSetting": "Test2",
    "Another": true
  }
}
MySettings.cs
public class MySettings
{
    public string SomeSetting { get; set; }
    public bool Another { get; set; }
}
Startup.cs
services.AddOptions();
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
HomeController.cs
public HomeController(IOptions<MySettings> config)
{
    string setting = config.Value.SomeSetting;
    bool another = config.Value.Another;
}

Configuration Sources

[Bearbeiten | Quelltext bearbeiten]

NuGet: Microsoft.Extensions.Configuration.Xml

appsettings.xml
<configuration>
  <MySettings>
    <SomeSetting>Test</SomeSetting>
    <Another>true</Another>
  </MySettings>
</configuration>
Startup.cs
app.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)

NuGet: Microsoft.Extensions.Configuration.Ini

appsettings.ini
[MySettings]
SomeSetting=TestIni
Another=true
Startup.cs
.AddIniFile("appsettings.ini", optional: true, reloadOnChange: true)

Environment Variables

[Bearbeiten | Quelltext bearbeiten]

NuGet: Microsoft.Extensions.Configuration.EnvironmentVariables

PowerShell
$env:'MySettings:SomeSetting' = "Setting value"
$env:'MySettings__Another' = "true"
$env:'MySettings:MoreSettings:ThirdSetting' = "Some value"
Startup.cs
app.AddEnvironmentVariables()

Command-Line Arguments

[Bearbeiten | Quelltext bearbeiten]

NuGet: Microsoft.Extensions.Configuration.CommandLine

PowerShell
. ./MyWebApp.exe --mysettings:somesetting "Some value from commandline"
Startup.cs
app.AddCommandLine(Environment.GetCommandLineArgs().Skip(1).ToArray())
Startup.cs
app.AddInMemoryCollection(new Dictionary<string, string>
{
    {"MySettings:SomeSetting", "MyValue" }
})

NuGet: Microsoft.Extensions.Configuration.UserSecrets

  • Storing developer-specific secrets
  • Use KeyVault in Azure
Assembly.cs
// use project.json for ASP.NET Core v1.0.0
[assembly: UserSecretsId("aspnet-TestApp-20170103063931")]
Startup.cs
builder.AddUserSecrets<Startup>();

|}