为什么 ModelState 返回不同的结果以及如何修复它?

Art*_*m G 3 validation asp.net-mvc modelstate asp.net-web-api asp.net-core

Asp.net core 3.1 WebApi。

我有一个具有所需属性的模型。

1.如果模型无效,则响应包含如下数据:

{
    "errors": {
        "Name": [
            "Update model can't have all properties as null."
        ],
        "Color": [
            "Update model can't have all properties as null."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|f032f1c9-4c36d1e62aa60ead."
}
Run Code Online (Sandbox Code Playgroud)

这对我来说看起来不错。

但是,如果我向 modelState.AddModelError("statusId", "Invalid order status id.") 添加一些自定义验证,那么它会返回不同的结构:

[
    {
        "childNodes": null,
        "children": null,
        "key": "statusId",
        "subKey": {
            "buffer": "statusId",
            "offset": 0,
            "length": 8,
            "value": "statusId",
            "hasValue": true
        },
        "isContainerNode": false,
        "rawValue": "11202",
        "attemptedValue": "11202",
        "errors": [
            {
                "exception": null,
                "errorMessage": "Invalid order status id."
            }
        ],
        "validationState": 1
    }
]
Run Code Online (Sandbox Code Playgroud)

看起来 ModelState.IsValid 对于控制器来说实际上不再有效,因为错误的请求甚至在进入无效模式的控制器之前就被返回了。或者是否有一些通过 ModelSate 进行全局验证的标志?

为什么结构不同?怎样才能让它一样呢?如何在 MVC 中强制访问 api 控制器内部的 ModelState.IsValid 方法?

更新:



    [Route("....")]
    [Authorize]
    [ApiController]
    public class StatusesController : ApiControllerBase
    {


        [HttpPut, Route("{statusId}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesResponseType(StatusCodes.Status409Conflict)]
        [Produces("application/json")]
        public async Task<ObjectResult> UpdateStatusAsync(int statusId, [FromBody] StatusUpdateDto orderStatusUpdateDto)
        {

            int companyId = User.Identity.GetClaimValue<int>(ClaimTypes.CompanyId);

            const string errorNotFound  = "There is not order status with this id for such company";
            if (statusId <= 0)
            {
                Logger.LogError(errorNotFound);
                ModelState.AddErrorModel(nameof(statusId), "Invalid order status id")
                throw new NotFound(ModelState);
            }
            
            if (orderStatusUpdateDto == null)
            {
                const string error = "Invalid (null) order status can't be added";
                Logger.LogError(error);
                throw new ArgumentNullException(error);
            }

            if (ModelState.IsValid == false) // also this code is always true or returns 400 before this line
            {
                return BadRequest(ModelState); 
            }

           ....
            return result;
        }

}
Run Code Online (Sandbox Code Playgroud)

tre*_*eze 7

ApiController属性向控制器添加了一些特定的固执己见的行为。其中之一是如果模型无效,则返回 400 错误。可以禁用此行为,但只能在全局级别上禁用。

services.AddControllers()
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressModelStateInvalidFilter = true;
    });
Run Code Online (Sandbox Code Playgroud)

我认为你有以下选择:

  • 禁用此行为并检查ModelState.IsValid自己。使用ValidationProblem方法产生相同的响应
  • 将此检查添加到模型的验证器中
  • 保持一切原样。但ValidationProblem在控制器方法内部使用返回验证错误。

有关信息,请参阅https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#automatic-http-400-responses