hinzufügen json
Datei zu Ihrem Projekt Wurzel Regie: config.json
{
"AppSettings": {
"TestKey" : "TestValue"
}
}
erstellen Sie ein neues class
für Config Deserialisierung:
public class AppSettings
{
public string TestKey { get; set; }
}
In Startup.cs
:
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
var builder = services.AddMvc();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
die Optionen in Ihrem controller
Get:
public HomeController(IOptions<AppSettings> settings)
{
var value = settings.Value.TestKey;
}
Unter 'References', fügte ich' System.Configuration (1.0.0) 'zu DNX 4.5.1. Allerdings bekomme ich immer noch "Der Typ oder Namespace 'Konfiguration' existiert nicht im Namespace 'System' (fehlt Ihnen eine Assembly oder Referenz?)" –