【问题标题】:Differing Web API ModelState error responses depending on data type根据数据类型不同的 Web API ModelState 错误响应
【发布时间】:2019-04-09 19:08:52
【问题描述】:

我们使用.NET Web APISQL Server。我从 API 获得的默认错误响应似乎因列的数据类型而异。在前端,我们有一个用户填写的表,它填充了上述数据库表。如果我为所有这些必填字段提交包含空白值的记录,这就是 API 在控制台中返回的内容(在“网络”-->“响应”选项卡中):

{
  "Message": "The request is invalid.",
  "ModelState": {
    "myTable": [
      "Required property 'columnInt1' not found in JSON. Path '', line x, position xxx.",
      "Required property 'columnInt2' not found in JSON. Path '', line x, position xxx.",
      "Required property 'columnInt3' not found in JSON. Path '', line x, position xxx.",
      "Required property 'columnDecimal' not found in JSON. Path '', line x, position xxx."
    ],
    "myTable.columnString1": [
      "The columnString1 field is required."
    ],
            ],
    "myTable.columnString2": [
      "The columnString2 field is required."
    ]
  }
}

请注意根据数据类型的不同错误响应。如果我将其中一个 varchar 列更改为 int 或 decimal 数据类型,其错误响应会更改以匹配其余 int/decimal 列具有 ("Required property 'columnInt1' not found in JSON. Path '', line x, position xxx") 的消息。

我希望所有错误都具有两个 varchar 列的响应("The stringColumn1 field is required." 等)。 有没有办法修改这个默认行为,为什么会这样?


下面是我们的表格设计示例:

Column Name:       Data Type:           Allow Nulls:
columnInt1              int                   no
columnInt2              int                   no
columnInt3              int                   no
columnDecimal           decimal(5, 2)         no
stringColumn1           varchar(4)            no
stringColumn2           varchar(5)            no

此表的 API 模型如下所示:

    [Required]
    public int columnInt1 { get; set; }

    [Required]
    public int columnInt2 { get; set; }

    [Required]
    public int columnInt3 { get; set; }

    [Required]
    [Precision(4,2)]
    public decimal columnDecimal { get; set; }

    [Required]
    [StringLength(4)]
    public string columnString1 { get; set; }

    [Required]
    [StringLength(5)]
    public string columnString2 { get; set; }

【问题讨论】:

    标签: sql-server database asp.net-web-api database-design model


    【解决方案1】:

    所以我找到了一种将 int 和 decimal 错误消息移动到更像字符串的方法。 AKA,我可以让 columnInt1 获得 ModelState 响应 The columnInt1 field is required

    它需要将可选参数?[Required] 属性结合起来,这对我来说似乎有点笨拙。谁能告诉我这可能会造成什么负面影响(如果有的话)?或者这到底是多么糟糕的做法?或者——也许这没什么大不了的?似乎对我的目的有用。

    这是我的模型在修复后的样子:

    [Required]
    public int? columnInt1 { get; set; }    // the ? seems contradictory to [Required], but fixes my issue
    
    [Required]
    public int? columnInt2 { get; set; }
    
    [Required]
    public int? columnInt3 { get; set; }
    
    [Required]
    [Precision(4,2)]
    public decimal? columnDecimal { get; set; }
    
    [Required]
    [StringLength(4)]
    public string columnString1 { get; set; }
    
    [Required]
    [StringLength(5)]
    public string columnString2 { get; set; }
    

    【讨论】:

    • 注意 - 我最终解决了这个问题而无需操作 modelState - 我们将其保留为,撤消对上面模型的更改并收到另一个错误消息(完全不相关且对本主题没有帮助) 方式。但是如果 Jon Skeet 碰巧突然出现,我仍然想了解更多关于上述特殊行为的信息......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多