C# Web API反序列化将int转换为字符串,如何防止它?

C# Web API反序列化将int转换为字符串,如何防止它?,c#,json,asp.net-mvc,asp.net-web-api2,C#,Json,Asp.net Mvc,Asp.net Web Api2,我有一个带有搜索后操作的API,它接受json的主体,其形式为 { "EventID": [ "1234-567-890", "1234-567-890", "1234-567-890" ], "boolFlag": 0 } 并将其反序列化为“SearchCriteria”对象 我遇到的问题是,如果我输入一个eventID作为整数1234,不带引号,这在技术上是有效的JSON。当我进入控制器操作时,eventID的值从12

我有一个带有搜索后操作的API,它接受json的主体,其形式为

{
    "EventID": [
        "1234-567-890",
        "1234-567-890",
        "1234-567-890"
    ],
    "boolFlag": 0
}
并将其反序列化为“SearchCriteria”对象


我遇到的问题是,如果我输入一个eventID作为整数1234,不带引号,这在技术上是有效的JSON。当我进入控制器操作时,eventID的值从1234变为“1234”,而不是使ModelState无效。并继续在代码的其余部分被视为字符串。我们不希望用户能够为EventID输入整数。它们一定是弦。有没有办法将此属性赋予属性和/或防止反序列化将int“强制转换”为字符串?

解决方法:可以使用此方法将字符串转换回int。然后可以执行以下操作:

public static void Main()
   {
      String[] values = { null, "160519", "9432.0", "16,667",
                          "   -322   ", "+4302", "(100);", "01FA", "11111", "1234", "1234-1234-1234" };
      foreach (var value in values) {
         int number;

         bool result = Int32.TryParse(value, out number);
         if (result == true)
         {
            Console.WriteLine("This input value is definitely not valid as it is a number.");         
         }
         else
         { 
            Console.WriteLine("Perhaps this can be a valid input value as it could not be parsed as integer.", 
                               value == null ? "<null>" : value);
         }
      }
   }

有关演示,请参见。

因为在您的类中,您已将其定义为字符串数组,所以它将始终反序列化为字符串。如果假定
1234
无效,则可能存在大量无效值。输入数据时验证数据。@MattBurland我正在尝试验证数据,但我不知道哪些字符串最初作为整数传入,哪些字符串应保留为字符串。我试图避免检查字符串是否包含“-”字符为什么这很重要?如果它最初是一个数字,那么它不符合事件ID的模式,并且是无效的。如果一个数字是有效的,那么无论如何你都应该把它当作一个字符串。因为如果你有一个数据字段,有时是一个字符串,有时是一个数字,那么你会问各种各样的问题。除非您打算对事件ID进行数学运算,否则您可能应该只使用字符串,然后您可能想将其推回去。如果有效格式为
\d{4}-\d{4}-\d{4}
,则检查该格式,如果事件id不适合该格式,则返回正确的错误消息。对于一个错误的请求返回200是一种可怕的用户体验。更糟糕的是,如果你有时不为一些错误的请求发送200,因为它们符合某种模式,而这种模式在某种程度上比用户不知道的其他无效模式“更糟糕”。那么你是说,tryParse,如果它成功返回int,那么它只是一个整数字符,这是无效的?嗯,这可能行得通……我唯一的问题是单元测试。对于单元测试,我们在测试类中创建了一个对象实例,因此我显然不能在字符串数组中输入整数,如果我输入一个被Int32.TryParse捕获的字符串“1234”,测试会意外失败@逻辑性我不确定我是否在听你的评论。不过,请看我的更新,让我知道你的想法。
public static void Main()
   {
      String[] values = { null, "160519", "9432.0", "16,667",
                          "   -322   ", "+4302", "(100);", "01FA", "11111", "1234", "1234-1234-1234" };
      foreach (var value in values) {
         int number;

         bool result = Int32.TryParse(value, out number);
         if (result == true)
         {
            Console.WriteLine("This input value is definitely not valid as it is a number.");         
         }
         else
         { 
            Console.WriteLine("Perhaps this can be a valid input value as it could not be parsed as integer.", 
                               value == null ? "<null>" : value);
         }
      }
   }
Perhaps this can be a valid input value as it could not be parsed as integer. // null
This input value is definitely not valid as it is a number. // "160519"
Perhaps this can be a valid input value as it could not be parsed as integer. // "9432.0"
Perhaps this can be a valid input value as it could not be parsed as integer. // "16,667"
This input value is definitely not valid as it is a number. // "   -322   "
This input value is definitely not valid as it is a number. // "+4302"
Perhaps this can be a valid input value as it could not be parsed as integer. // "(100);",
Perhaps this can be a valid input value as it could not be parsed as integer. // "01FA"
This input value is definitely not valid as it is a number. // "11111"
This input value is definitely not valid as it is a number. // "1234"
Perhaps this can be a valid input value as it could not be parsed as integer. // "1234-1234-1234"