title: ModelState is always valid in ASP.NET.CORE 2.2 Web Api
body: <p>I have an Asp.Net Core 2.2 web api project.Recently i tried to add validation on the models by adding DataAnnotation or the FluentValidation library.</p>
  
  <p>In my Unit tests though i can see that even passing invalid model values the model state is valid.</p>
  
  <p>StartUp.cs</p>
  
  <pre><code>services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
          .AddFluentValidation();
  
  services.AddTransient&lt;IValidator&lt;ClientDto&gt;, ClientValidator&gt;();
  </code></pre>
  
  <p>ClientController</p>
  
  <p>My Controller inherits from the ControllerBase and has the [ApiController] attribute.</p>
  
  <pre><code>    [HttpPost]
      public async Task&lt;IActionResult&gt; Create([FromBody] ClientDto client)
      {
  
          if (!ModelState.IsValid)
              return BadRequest();
  
          await _clientsService.Create(client);
  
          var clientAdded = await _clientsService.GetCustomer(c =&gt; c.IntegralFileName == client.IntegralFileName);
  
          return CreatedAtAction("Create", client, clientAdded);
  
      }
  </code></pre>
  
  <p>ClientDto.cs</p>
  
  <pre><code> public class ClientDto
  {
      public string Id { get; set; }
  
      public string FirstName { get; set; }
  
      public string LastName { get; set; }
  
      public bool Admin { get; set; }
  
      public bool Active { get; set; }
  }
  </code></pre>
  
  <p>ClienValidator.cs</p>
  
  <pre><code>public class ClientValidator : AbstractValidator&lt;ClientDto&gt;
  {
      public ClientValidator()
      {
          RuleFor(x =&gt; x.Id).NotNull();
          RuleFor(x =&gt; x.FirstName).Length(4, 20);
          RuleFor(x =&gt; x.LastName).Length(3, 20);
      }
  }
  </code></pre>
  
  <p>I think i tried everything , some of them:</p>
  
  <p>1)Removed Fluent Validation and replace it with DataAnnotations</p>
  
  <p>2)Replace AddMcv with</p>
  
  <pre><code>   services.AddMvcCore()
              .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
              .AddJsonFormatters()
              .AddApiExplorer()
              .AddAuthorization()
              .AddDataAnnotations()
              .AddFluentValidation(fv =&gt; fv.RegisterValidatorsFromAssemblyContaining&lt;ClientValidator&gt;());
  </code></pre>
  
  <p>But i cant see any difference in the ModelState value.
  Any Ideas??</p>
  
  <p>Thanks</p>