Add Exists
to IConfigurationSection #292
Description
Right now, when you get a configuration section using GetSection()
there is no simple way to figure out whether that section actually exists. The only thing you could do is check if the value is non-null or if the count of GetChildren()
is larger than zero:
IConfigurationSection section = config.GetSection("Some:Section");
bool exists = section.Value != null || section.GetChildren().Count() > 0;
And this doesn’t even work if the section was actually set with a value of null
on purpose.
I would like to see some Exists
method/property that tells me whether the queried section exists in the configuration. Otherwise, it’s difficult to detect optional configuration parts.
As an example, I want a configuration section Credentials
where credentials can be specified. If they are not specified, then that’s fine too. So the configuration may look like this:
"Credentials": {
"UserName": "username",
"Password": "password"
}
Or the section may be completely missing. So I get the configuration section using GetSection
and now, I want to detect whether that section exists in the configuration or not.