When a route/querystring parameter is model binded, it only works if the value is the exact casing (aka. case sensitive).
Enums model binding should be case -insensitive-
Expected Behavior
Case insensitive model binding for enum's.
e.g
GET/optiona resolves the endpoint and returns the string OptionA
Steps To Reproduce
dotnet new webapi -minimal
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(o =>
{
o.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
o.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
o.SerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.MapGet("/{myenum}", ([FromRoute] MyEnum myenum) =>
{
return Results.Ok(myenum.ToString());
});
app.Run();
public enum MyEnum
{
OptionA,
OptionB
}
Navigate to /optiona and be shown an error
BadHttpRequestException: Failed to bind parameter "MyEnum myenum" from "optiona".
Navigate to /OptionA and see "OptionA" in the response body of the browser.
Exceptions (if any)
N.A.
.NET Version
8.0.303
Anything else?
This is a duplicate of #45590 which is now closed 😢 It was requiring human feedback from the author but never got it so it's auto closed.
I think it should be reopened and the suggested workaround not a fair suggestion at all. I feel like this should be default to case insensitive with a possible "setting" to make it either sensitive-or-insensitive
Thanks @martincostello - i'll now subscribe to #52497 and 🙏🏻 it slips into a vnext not too long in the future. Cheers and keep fighting the good fight!
Is there an existing issue for this?
Describe the bug
When a route/querystring parameter is model binded, it only works if the value is the exact casing (aka. case sensitive).
Enums model binding should be case -insensitive-
Expected Behavior
Case insensitive model binding for enum's.
e.g
GET/optiona resolves the endpoint and returns the string OptionA
Steps To Reproduce
Navigate to /optiona and be shown an error
Navigate to /OptionA and see "OptionA" in the response body of the browser.
Exceptions (if any)
N.A.
.NET Version
8.0.303
Anything else?
This is a duplicate of #45590 which is now closed 😢 It was requiring human feedback from the author but never got it so it's auto closed.
I think it should be reopened and the suggested workaround not a fair suggestion at all. I feel like this should be default to case insensitive with a possible "setting" to make it either sensitive-or-insensitive
Visual Studio (latest community edition)