3

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Torben Nielsen
  • 663
  • 1
  • 8
  • 21
  • 1
    Have a look at here: https://stackoverflow.com/questions/8922930/net-custom-configuration-how-to-case-insensitive-parse-an-enum-configurationpro – Hassan Monjezi Nov 19 '22 at 12:07
  • @HassanMonjezi That's for the `.config` XML-based configuration system in .NET Framework - it isn't relevant to the OP's problem (which is ASP.NET Core enum value binding in .NET 7). – Dai Nov 19 '22 at 13:48
  • Please clarify: do you want enum binding to be case-sensitive or case-insensitive? (Your question title says you want case-insensitive, but your question body says you want case-sensitive behaviour). – Dai Nov 19 '22 at 13:49
  • I would like enum binding to be case-insensitive – Torben Nielsen Nov 19 '22 at 14:35
  • BTW, as a stopgap you could use an IIS `` rule to correct the casing by rewriting case-insensitive input requests to correctly-cased URIs. – Dai Nov 19 '22 at 15:38

1 Answers1

3

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.

  • If the links break at some point in the future you answer loses most of its usefullness. Try to avoid link only answers. Summarise what information the links provide. – moken May 17 '23 at 03:06