5

I want to completely disable model validation for WebAPI controllers. I tried a few ways to do it for MVC, but seems WebAPI doesn't get that methods.

In my case:

  1. custom formatter creates and populates object
  2. default validation happens
  3. Object passed to controller
  4. My code start working

I'm trying to completely remove step 2.

3
  • can u clarify further. do you have data annotations setup on your poco classes? do you have code in http verb methods that look like if(ModelState.IsValid)? if so you should remove that. Maybe I'm not fully understanding you. Commented May 22, 2013 at 18:42
  • I`m trying to completely remove default validation, not just ignore it. Commented May 23, 2013 at 12:47
  • Not enough upvotes on this question ;-) This validation, we didn't know or care about, was automatically running every method in every object we sent, killing performance by about 3000% (confirmed via profiling). Very relieved to turn it off. Commented Feb 9, 2015 at 5:21

1 Answer 1

13

Try this:

config.Services.Clear(typeof(ModelValidatorProvider));

It should completely disable validation both for URI parameters and for body parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

It throws exception "ModelValidatorProvider is not supported", so I removed System.Web.Http.Validation.IBodyModelValidator and it works! Thanks.
On thing to note, this needs to be called before EnsureInitialized() or the route bindings will be setup with the default validator.
One more thing. It is the System.Web.Http.Validation.ModelValidatorProvider

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.