Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hook FluentValidator to a Web API?

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 ?

like image 856
Marty Avatar asked Oct 19 '12 13:10

Marty


People also ask

What is FluentValidation C#?

FluentValidation is a .NET library for building strongly-typed validation rules. It Uses a fluent interface and lambda expressions for building validation rules. It helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic.

What is FluentValidation Aspnetcore?

Fluent Validation is a free to use . NET validation library that helps you make your validations clean, easy to create, and maintain. It even works on external models that you don't have access to, with ease. With this library, you can separate the model classes from the validation logic like it is supposed to be.

How does fluent validation works?

FluentValidation is a server-side library and does not provide any client-side validation directly. However, it can provide metadata which can be applied to the generated HTML elements for use with a client-side framework such as jQuery Validate in the same way that ASP. NET's default validation attributes work.


1 Answers

latest version of Fluent Validation (5.0.0.1) supports web api

Just install it from Nuget and register it in Global.asax like so:

using FluentValidation.Mvc.WebApi;  public class WebApiApplication : System.Web.HttpApplication {     protected void Application_Start()     {         ...         FluentValidationModelValidatorProvider.Configure();     } } 
like image 138
Dmitry Efimenko Avatar answered Sep 23 '22 09:09

Dmitry Efimenko