如何区分模型绑定错误和模型验证错误? [英] How to discern between model binding errors and model validation errors?

查看:174
本文介绍了如何区分模型绑定错误和模型验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core MVC 2.0实现REST API项目,如果模型绑定失败(因为请求在语法上是错误的),我想返回400状态代码;如果模型验证,我想返回422状态代码失败(因为该请求在语法上正确无误,但包含不可接受的值).

I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is syntactically correct but contains unacceptable values).

例如,给定类似操作

[HttpPut("{id}")]
public async Task<IActionResult> UpdateAsync(
    [FromRoute] int id,
    [FromBody] ThingModel model)

当路由中的id参数包含非数字字符或未在请求中指定任何正文时,我想返回400状态代码,而当ThingModel的属性时返回422状态代码包含无效值.

I'd like to return a 400 status code when the id parameter in the route contains a non-digit character or when no body has been specified in the request and a 422 status code when the properties of ThingModel contain invalid values.

从我所见,IValueProviderIModelBinder实现都像验证程序一样将错误添加到请求的ModelStateDictionary中,并且没有办法在绑定和验证之间注入代码.

From what I've seen both IValueProvider and IModelBinder implementations add their errors to the request's ModelStateDictionary like the validators do, and there is no way to inject code between binding and validation.

如何实现这种行为?

推荐答案

尚未检查选项#1,但是:

  • ModelState.此处)以自动返回新的BadRequestObjectResult(actionContext.ModelState)-因为在绑定错误且绑定值为NULL的情况下,无需执行任何操作,并且验证错误的情况下,我们也可能无能为力.
  • ModelState.ValidationState has 4 possible values (Unvalidated, Invalid, Valid, Skipped) where in case of model binding errors I do get Unvalidated as a value
  • Also would consider using ApiBehaviorOptions (see sample here) to automatically return a new BadRequestObjectResult(actionContext.ModelState) - since in case of binding error with a NULL bound value there's nothing to do and in case of validation errors we probably can't do anything either.

ApiBehaviorOptions的快速注释:

  • 必须使用ApiController属性(它也需要在控制器级别上具有路由属性,并且确实会改变绑定的工作方式)
  • 具有ApiController属性的默认行为将返回BadRequestObjectResult(actionContext.ModelState),而无需任何额外的代码和配置
  • 如果您决定使用自己的ApiBehaviorOptions-必须在service.AddMvc之后对其进行初始化,或者需要使用具有类似效果的services.PostConfigure(o => {})
  • 为了使InvalidModelStateResponseFactory正常工作,ApiBehaviorOptions SuppressModelStateInvalidFilter必须为false
  • must use ApiController attribute (which requires also routing attribute on the controller level also and does alter the way binding works)
  • the default behaviour with ApiController attribute will return a BadRequestObjectResult(actionContext.ModelState) without any extra code and configuration
  • if you decide to roll your own ApiBehaviorOptions - you must initialize it after service.AddMvc or need to use: services.PostConfigure(o => {}) which has similar effect
  • ApiBehaviorOptions SuppressModelStateInvalidFilter has to be false for the InvalidModelStateResponseFactory to work

因此,在某些情况下,自定义过滤器是更好的解决方案(更改较少).

So in some cases a custom filter is a better solution (less changes).

这篇关于如何区分模型绑定错误和模型验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆