title: Intercept bad requests before reaching controller in ASP.NET Core
body: <p>I have a logic to apply in case the request received is a BadRequest, to do this I have created a filter:</p>
  
  <pre><code>public class ValidateModelAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext context)
      {
          if (!context.ModelState.IsValid)
          {
              // Apply logic
          }
      }
  }
  </code></pre>
  
  <p>In Startup:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddMvc(options =&gt; { options.Filters.Add&lt;ValidateModelAttribute&gt;(); });
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[Route("api/[controller]")]
  [ApiController]
  public class VerifyController : ControllerBase
  {
      [Route("test")]
      [HttpPost]
      [ValidateModel]
      public ActionResult&lt;Guid&gt; validationTest(PersonalInfo personalInfo)
      {
          return null;
      }
  }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public class PersonalInfo
  {
      public string FirstName { get; set; }
      [RegularExpression("\\d{4}-?\\d{2}-?\\d{2}", ErrorMessage = "Date must be properly formatted according to ISO 8601")]
      public string BirthDate { get; set; }
  }
  </code></pre>
  
  <p>The thing is when I put a break point on the line:</p>
  
  <pre><code>if (!context.ModelState.IsValid)
  </code></pre>
  
  <p>execution reaches this line only if the request I send is valid. Why it is not passing the filter if I send a bad request?</p>