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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Oleh Nechytailo
  • 2,155
  • 17
  • 26
  • 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. – origin1tech May 22 '13 at 18:42
  • I`m trying to completely remove default validation, not just ignore it. – Oleh Nechytailo May 23 '13 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. – PandaWood Feb 09 '15 at 05:21

1 Answers1

13

Try this:

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

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

Youssef Moussaoui
  • 12,187
  • 2
  • 41
  • 37
  • 1
    It throws exception "ModelValidatorProvider is not supported", so I removed System.Web.Http.Validation.IBodyModelValidator and it works! Thanks. – Oleh Nechytailo May 23 '13 at 12:45
  • On thing to note, this needs to be called before EnsureInitialized() or the route bindings will be setup with the default validator. – michael.aird Jan 14 '14 at 15:00
  • One more thing. It is the System.Web.Http.Validation.ModelValidatorProvider – danicomas Feb 19 '18 at 16:23