erstellt
Code: Select all
{
"SectionA": {
"SectionASubItem1": "test value",
"SectionASubItem2": "test value2"
},
"SectionB": {
"SectionBSubItem1": "test value",
"SectionBSubItem2": "test value2"
}
}
So sieht meine MyConfiguration Klasse aus:
Code: Select all
public class MyConfiguration
{
public SectionA SectionA { get; set; }
public SectionB SectionB { get; set; }
}
public class SectionA
{
public string SectionASubItem1 { get; set; }
public string SectionASubItem2 { get; set; }
}
public class SectionB
{
public string SectionBSubItem1 { get; set; }
public string SectionBSubItem2 { get; set; }
}
Code: Select all
var myConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
< /code>
und Zuordnung des Abschnitts wie folgt: < /p>
builder.Services.Configure(myConfiguration.GetSection("SectionA"));
builder.Services.Configure(myConfiguration.GetSection("SectionB"));
Code: Select all
public readonly MyConfiguration configuration;
public WeatherForecastController()
{
configuration = new MyConfiguration();
}
[HttpGet(Name = "GetWeatherForecast")]
public IActionResult Get()
{
// Here I am getting null
var sectionA = configuration.SectionA;
if (sectionA == null)
return BadRequest();
return Ok();
}