-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to bind Dictionary with key type "Enum" when key string case is different #71185
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsDescriptionHello.
I suppose that enum keys should be handled the same way as strings. runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs Line 547 in 95d64e8
Reproduction StepsCreate settings class: public class TestConf : Dictionary<TestEnum, string>
{
}
public enum TestEnum
{
Test1,
Test2,
} Modify appsettings.json: "TestConf": {
"Test1": "foo",
"test2": "bar"
} Register a configuration in Program.cs: builder.Services.Configure<TestConf>(builder.Configuration.GetSection(nameof(TestConf))); And finally try somewhere to get options value: [HttpGet("test")]
public async Task<IActionResult> TestAppsettingsForIssue([FromServices] IOptionsSnapshot<TestConf> testConf)
{
var t = testConf.Value;
return Ok();
} I am not sure if this is a bug or intentional behavior. Expected behaviorSince ASP.NET Core documentation says Actual behaviorIt throws an error Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Description
Hello.
Since ASP.NET Core documentation says:
https://github.com/dotnet/AspNetCore.Docs/blob/c875045a940e6ce3d24c24573309ff4be71d4fde/aspnetcore/fundamentals/configuration/index.md?plain=1#L404
I suppose that enum keys should be handled the same way as strings.
But at the moment ConfigurationBinder throws an error as it uses "Enum.Parse" method:
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Line 547 in 95d64e8
Reproduction Steps
Create settings class:
Modify appsettings.json:
Register a configuration in Program.cs:
And finally try somewhere to get options value:
I am not sure if this is a bug or intentional behavior.
Expected behavior
Since ASP.NET Core documentation says
Configuration keys are case-insensitive
it should not throw an error.Actual behavior
It throws an error
System.ArgumentException: Requested value 'test2' was not found.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: