WebAPI takes a different value for integer

I developed a WebAPI that accepts the class and type of the mail method. I came across a really strange scenario here.

I post below json.

{
 "MessageID":"fec41da8-655c-4532-af7d-554e1505a663",
 "Number":0009,
}

In the method, I get Number as 0 - but if I go from 0001 to 0008 - it works fine, and I accept others as 8. Only for 0009 it converts to 0 for other values, it removes the other 0.

0002 receives as 2
0009 receives as 0

Any idea?

+4
source share
2 answers

The reason I figure here is your JSONinvalidJSON

{
    "MessageID":"fec41da8-655c-4532-af7d-554e1505a663",
    "Number":0009,
}

Valid JSON will be

{
  "MessageID": "fec41da8-655c-4532-af7d-554e1505a663",
  "Number": "0009"
}

JSON , ,

public class Test
{
    public Guid MessageId { get; set; }
    public int Number { get; set; }
}

WEB API 2.0, .

Number 0 - 0001 0008 - , 8. 0009 0 , 0.

, JSON, 0 , 0001 to 0008 or 0009.

+1

json .

long value2 = text2.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? 
              Convert.ToInt64(text2, 16) : 
              Convert.ToInt64(text2, 8); // Here OCTAL!!!
-1

Source: https://habr.com/ru/post/1668929/


All Articles