Search code examples
c#asp.net.net-7.0

Can enum query parameters be case-insensitive?


When we migrated our application from .NET 6 to .NET 7, we found that enums used as query parameters had changed to being case-sensitive. We quickly started getting complaints from downstream systems, and had to fallback to .NET 6.

Is there a way to change whether query parameters are case sensitive?

For a controller like this

[ApiController] [Route("api/[controller]")]
public class EnumController : ControllerBase
{
    [HttpGet]
    public ActionResult<string> Get(MyValues myValue)
    {
        return myValue.ToString();
    }
}

public enum MyValues
{
    FirstValue,
    SecondValue,
    ThirdValue
}

This query-parameter will work in both .NET 6 and .NET 7:

?myValue=FirstValue

However this query parameter will only work in .NET 6

?myValue=firstvalue

In .NET 7 we get this error:

The value 'firstvalue' is not valid
"Type":"https://tools.ietf.org/html/rfc7231#section-6.5.1"
One or more validation errors occurred


Solution

  • I stumbled into the exact same issue, and it's related to the new TryParseModelBinder introduced in .NET 7.

    The issue is fixed in https://github.com/dotnet/aspnetcore/pull/46577 so if you update to .NET 7.0.5 it should work as intended and not be case sensitive.