Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Add Custom ModelValidatorProviders To Web API Project?

I'm moving some MVC code to Web API and I need to update my custom ModelValidatorProviders. It seems as though my validators can stay much the same only they should inherit the System.Web.Http.Validation namespace.

What I can't figure out is how to add the provider to Web API. When using MVC I can just add the following to my global.asax:

ModelValidatorProviders.Providers.Add(new CustomModelValidatorProvider());

How do I use the custom provider with Web API?

like image 306
Mark Avatar asked Oct 15 '12 22:10

Mark


People also ask

Which service is used for adding controller in Web API?

ASP.NET Core supports creating web APIs using controllers or using minimal APIs.

What is HttpConfiguration in Web API?

The HttpConfiguration is the main class which includes following properties using which you can override the default behaviour of Web API. Property. Description. DependencyResolver. Gets or sets the dependency resolver for dependency injection.


1 Answers

This page Configuring ASP.NET Web API helped me answer my own question. Specifically this is what I ended up doing:

GlobalConfiguration.Configuration.Services.Add(typeof(ModelValidatorProvider), new CustomModelValidatorProvider());
like image 188
Mark Avatar answered Sep 30 '22 08:09

Mark