model validation for webapi
2020-12-29 12:27
标签:action icon read des core ems with as rac mic Model validation in ASP.NET Core MVC and Razor Pages ModelStateInvalidFilter Class webapi:Create web APIs with ASP.NET Core The ASP.NET Core MVC uses the ModelStateInvalidFilter action filter to do the preceding check. 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: 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: The See How to log automatic 400 responses on model validation errors (aspnet/AspNetCore.Docs #12157). model validation for webapi 标签:action icon read des core ems with as rac mic 原文地址:https://www.cnblogs.com/panpanwelcome/p/13295264.htmlAutomatic HTTP 400 responses
[ApiController]
attribute makes model validation errors automatically trigger an HTTP 400 response. Consequently, the following code is unnecessary in an action method:if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Default BadRequest response
{
"": [
"A non-empty request body is required."
]
}
{
"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."
]
}
}
ValidationProblemDetails
type:
Log automatic 400 responses
文章标题:model validation for webapi
文章链接:http://soscw.com/index.php/essay/39050.html