Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 2.1 Override Automatic Model Validation

In the latest .NET Core 2.1, an automatic validation for the model state validation is introduced (https://blogs.msdn.microsoft.com/webdev/2018/02/02/asp-net-core-2-1-roadmap/#mvc).

Previously I could override the validation error response by the following code below:

public class ApiValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            context.Result = new BadRequestObjectResult(new context.ModelState);
        }

    base.OnActionExecuting(context);
}

But now it no longer works. The validation errors is responded without entering the override method.

Anyone has any clue? Thanks.

like image 377
Kes Avatar asked Jul 01 '18 17:07

Kes


People also ask

Which of the following are alternatives to perform model validation instead of using built in validation attributes?

Alternatives to built-in attributes If you need validation not provided by built-in attributes, you can: Create custom attributes. Implement IValidatableObject.

How does ModelState IsValid work?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What is ModelState in asp net core?

What is a ModelState C#? When we talk about ModelState , we mean ModelState property of the ControllerBase abstract class in the Microsoft. AspNetCore. Mvc namespace. It is of ModelStateDictionary type and it represents errors that come from two subsystems: model binding and model validation.

What is ModelState C#?

ModelState is a property of a Controller instance, and can be accessed from any class that inherits from Microsoft. AspNetCore. Mvc. Controller. The ModelState has two purposes: to store and submit POSTed name-value pairs, and to store the validation errors associated with each value.


2 Answers

If you'd like to keep using the ApiController attribute (which has other functions like disabling conventional routing and allowing model binding without adding [FromBody] parameter attributes), you might be able to do it via this in your Startup.cs file:

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

That will make it so that if the ModelState is invalid it won't automatically return a 400 error.

like image 75
Zout Avatar answered Sep 27 '22 18:09

Zout


I was recently asked by a friend about this and my approach was to replace the default ModalStateInvalidFilter by a custom one.

In my test I've implemented the suggestion from here and then:

services.AddMvc(options =>
{
    options.Filters.Add(typeof(ValidateModelAttribute));
});


services.Configure<ApiBehaviorOptions>(options => { options.SuppressModelStateInvalidFilter = true; });
like image 25
Daniel Teleginski Camargo Avatar answered Sep 27 '22 17:09

Daniel Teleginski Camargo