Why is Request.Content.ReadAsAsync case insensitive?
Suppose I have some useful json information sent from client side as shown below.
{"Number": 2, "number": 4}
On the server side, I have this model class.
public class Arg
{
public int Number { get; set; }
}
The payload is deserialized in my controller like this:
Request.Content.ReadAsAsync<Arg>();
Why Arg.Number
== 4? How can I make it ReadAsAsync
case sensitive?
source to share
Deserialization is done through Json.NET. I dig in the process and end up with the following code:
public JsonProperty GetClosestMatchProperty(string propertyName)
{
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
if (property == null)
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
return property;
}
So, as you can see, if he can not get the property by using String.Ordinal
the component of, he will try String.OrdinalIgnoreCase
, and that's why it overrides your value.
For the name, I only see one solution to add a dummy property to catch this value:
public class Arg
{
[JsonProperty("Number")]
public int Number { get; set; }
[JsonProperty("number")]
public int SmallNumber { get; set; }
}
source to share
You need to add a new property to your own deserializer
public class Arg
{
public int Number { get; set; }
public int number { get; set; }
}
Tested with newtonsoft
Trying to define your class as a broken job
public class Arg
{
[JsonProperty("Number")]
public int Number { get; set; }
}
source to share