ASP.NET Core Action Filter Doesn't Get Called

N P

I have an ASP.NET Core API (.Net Core 2.1) and I implemented an Action Filter using this article

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.1#action-filters

In my Model, I use Data Annotations to validate the model, and I added the ValidateModel attribute for the Action in my Controller.

    [HttpPost("CreateShipment")]
    [ValidateModel]
    public IActionResult CreateShipment([FromBody] CreateShipmentRequest request)
    {
         if (ModelState.IsValid)
         {
            //Do something
         }
         return Ok();
    }

I used Postman to test this, and my Action Filter gets called only if the Model is valid. If my request is missing a required field or some value is out of range, Action Filter doesn't get called. Instead I receive a 400 bad request with the model state in the response.

I implemented the Action Filter because I want to customize my model validation error. My understanding is that Action Filters get called at the time of model binding. Can someone help me figure out why this is happening and how to get the Action Filter to work?

UPDATE: I found the solution 2 seconds after posting the question, and the link @Silvermind posted below is great info too.

I added the following line to my Startup.cs

services.Configure<ApiBehaviorOptions>(options =>
{
     options.SuppressModelStateInvalidFilter = true;
});

It's well documented here on the Microsoft site. https://docs.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#automatic-http-400-responses

N P

Adding the following line to Startup.cs, ConfigureServices() method resolved the issue. turns out .Net Core has automatic 400 responses enabled by default. If you want to add custom Action Filters, you need to set those options at the startup.

services.Configure<ApiBehaviorOptions>(options =>
{
      options.SuppressModelStateInvalidFilter = true;
});

It's well documented here on the Microsoft site:

https://docs.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#automatic-http-400-responses

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET MVC Why doesn't this post action get called from the view?

JWT doesn't get stored in ASP.NET Core with Blazor

asp-action in ASP.NET Core 3.1 doesn't work with endpoint routing

ASP.NET CORE MVC: `asp-action` doesn't create the `href` attribute

How do I get Configuration, Cookie and DBContext in Action Filter in ASP.Net Core 2.x

Changing default action doesn't work for ASP.NET Core 2

ASP.NET Core redirect from POST action to url doesn't work

Invoke ASP.Net Core MVC controller action with parameter doesn't work

How to test custom action filter in ASP.NET Core?

ASP.NET Core 5 add action filter with parameter

Compress filter at the action level for ASP.NET Core MVC

ASP.NET Core 2.2 - Action Filter db Query Question

ASP.NET Core (MVC): Can't get to the Login HttpPost action in controller

Why doesn't my function action get called?

Redux - My reducers doesn't get called by my action

Why doesn't $.get Url.Action reach ASP.NET MVC async Controller?

ASP.NET Core - DI - Using Action<T> or IOption<T>

Unable to get custom attributes in asp.net action filter

Javascript redirect (window.location.href) doesn't work in an Asp.NET Core project when called from an EventListener function

ICollection doesn't show up on ASP.NET core web api get request

Perform a get action with asp.net core 2.1

Asp.net Core 3.1 Binding: Action with a Dictionary as parameter in GET

ASP.NET Core Pass Enumerable of objects to Get Action on Controller

How to get custom attribute name for action in asp.net core?

Get out the debug before entering the action in ASP.NET Core

Can't cancel task in Asp.net core Action

Action doesn't come to a head in Asp.Net MVC 4

Run code when action is called in specific controller asp.net core

Controller action method not being called making a post request in ASP.NET Core