问题描述
我试图钩流利的验证我的MVC Web API项目,它不是不想做的工作。
当我使用 myController的:控制器
- >工作正常( ModelState.IsValid
收益假
)
但是当我使用 myController的:ApiController
...没什么
有没有人对如何挂钩的经验?
答案是这个<一个href=\"https://fluentvalidation.$c$cplex.com/SourceControl/network/forks/havard/webapisupport/contribution/2253\">pull申请。
基本上你需要实现自定义的 ModelValidation
人员。
和一对夫妇更多的事情需要注意:
-
网页API做modelValidator从System.Web.Mvc命名空间在这里指出的不行,只能用从System.Web.Http的那些:
Server端验证定制DataAnnotationsModelValidatorProvider
-
您不要添加这样的:
ModelValidatorProviders.Providers.Add(新WebApiFluentValidationModelValidatorProvider());`
但像这样的:
<$p$p><$c$c>GlobalConfiguration.Configuration.Services.Add(typeof(System.Web.Http.Validation.ModelValidatorProvider),新WebApiFluentValidationModelValidatorProvider());`
I'm trying to hook Fluent Validation to my MVC WEB Api project, and it doesn't wanna work.
When I use MyController : Controller
-> works fine (ModelState.IsValid
returns False
)
but when I use MyController :ApiController
... nothing.
Does anyone have experience on how to hook those up ?
The answer is in this pull request.
Basically You need to implement custom ModelValidation
Provider.
And a couple more things to note:
Web API don't work with modelValidator from System.Web.Mvc namespace, only with the ones from System.Web.Http as noted here:
Server side validation with custom DataAnnotationsModelValidatorProvider
You don't add it like this:
ModelValidatorProviders.Providers.Add(new WebApiFluentValidationModelValidatorProvider());`
BUT like this:
GlobalConfiguration.Configuration.Services.Add(typeof(System.Web.Http.Validation.ModelValidatorProvider), new WebApiFluentValidationModelValidatorProvider());`
这篇关于如何挂钩FluentValidator到Web API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!