Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Action Filter Doesn't Get Called

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

like image 527
N P Avatar asked Mar 07 '19 18:03

N P


People also ask

What is action filter in ASP.NET Core?

Action filters – They run right before and after the action method execution. Exception filters – They are used to handle exceptions before the response body is populated. Result filters – They run before and after the execution of the action methods result.

How do I register an action filter globally in Web API?

Short answer: MVC and Web API filters are not cross compatible, and if you want to register them globally, you must use the appropriate configuration classes for each. Long answer: ASP.NET MVC and Web API are purposely designed to work in a similar way, but they are in fact different creatures.


2 Answers

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

like image 104
N P Avatar answered Sep 16 '22 23:09

N P


The [ApiController] attributes performs model validation automatically and triggers an HTTP response of 404, in .Net Core 3.0 you can chain to the new AddControllers() to suppress this feature:

services.AddControllers()
                .ConfigureApiBehaviorOptions(options =>
                {
                    options.SuppressModelStateInvalidFilter = true;
                });
like image 35
az6bcn Avatar answered Sep 19 '22 23:09

az6bcn