model validation for webapi

Posted 晴天彩虹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了model validation for webapi相关的知识,希望对你有一定的参考价值。

Model validation in ASP.NET Core MVC and Razor Pages

 

ModelStateInvalidFilter Class

 

webapi:Create web APIs with ASP.NET Core

Automatic HTTP 400 responses

The [ApiController] attribute makes model validation errors automatically trigger an HTTP 400 response. Consequently, the following code is unnecessary in an action method:

C#
if (!ModelState.IsValid)
{
    return BadRequest(ModelState);
}

ASP.NET Core MVC uses the ModelStateInvalidFilter action filter to do the preceding check.

Default BadRequest response

With a compatibility version of 2.1, the default response type for an HTTP 400 response is SerializableError. The following request body is an example of the serialized type:

JSON
{
  "": [
    "A non-empty request body is required."
  ]
}

With a compatibility version of 2.2 or later, the default response type for an HTTP 400 response is ValidationProblemDetails. The following request body is an example of the serialized type:

JSON
{
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "|7fb5e16a-4c8f23bbfc974667.",
  "errors": {
    "": [
      "A non-empty request body is required."
    ]
  }
}

The ValidationProblemDetails type:

  • Provides a machine-readable format for specifying errors in web API responses.
  • Complies with the RFC 7807 specification.

Log automatic 400 responses

See How to log automatic 400 responses on model validation errors (aspnet/AspNetCore.Docs #12157).

以上是关于model validation for webapi的主要内容,如果未能解决你的问题,请参考以下文章

go for range 踩坑

pytorch 笔记:validation ,model.eval V.S torch.no_grad

使用 model.fit() 和 validation_split 后获取训练/测试数据

将 cross_validation 算法转换为 model_selection

Django 表单 is_valid() 失败

如何避免短路评估