Answer the question
In order to leave comments, you need to log in
How to override inline validation in .net core request processing pipeline?
Good afternoon,
In .net core, the api controller is inherited from ControllerBase and the [ApiController] attribute is added to it, which contains some ready-made functionality.
But at the same time, validation is already built into the request processing pipeline. I would like to still use the [ApiController] attribute but use my own validation handler (e.g. return 422 error code instead of 400)
[Route("api/[controller]/[action]")]
[AllowAnonymous]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IAuthService service;
public AuthController(IAuthService service)
{
this.service = service;
}
[HttpPost]
[Produces(typeof(UserDto))]
public async Task<IActionResult> LogIn([FromBody] LoginDto model)
{
var user = await service.LogIn(model);
await SignInAsync(user);
return Ok(user);
}
}
public class LoginDto
{
[Required]
public string Login { get; set; }
[Required]
public string Password { get; set; }
}
{
"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title":"One or more validation errors occurred.",
"status":400,
"traceId":"00-a0b13ea44a9cb5250a7f22771528056d-e04f1f934fc804a6-00",
"errors":{"Login":["The Login field is required."]}
}
Answer the question
In order to leave comments, you need to log in
I found the answer on my own,
We are talking about an automatic response if the request has an invalid model https://docs.microsoft.com/en-us/aspnet/core/web-a...
And the same below shows how to remove this handler:
https://docs.microsoft.com/en-us/aspnet/core/web-a...
builder.Services.AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressConsumesConstraintForFormFileParameters = true;
options.SuppressInferBindingSourcesForParameters = true;
options.SuppressModelStateInvalidFilter = true; // вот это
options.SuppressMapClientErrors = true;
options.ClientErrorMapping[StatusCodes.Status404NotFound].Link =
"https://httpstatuses.com/404";
});
Something early you switched to web api )
Personally, I use the FluentValidation API (Not to be confused with Fluent Validator), which you can install via NuGet and see on the offsite how to set it up correctly, it takes literally up to an hour of work.
You can also use the standard Model property inside the controller and the IsValid property from it to validate the model, or separately check each property/field for a specific value (== null, is null, String.IsEmpty(...)) and return the corresponding netcode, for example, in your case, you can do return BadRequest( new { response = "Some fields is empty });
BadRequest refers to types that are available by the standard and in asp.net have corresponding netcode values. You use in example Ok, i.e. network code 200.
Also try to put on the HttpPost method - IgnoreAntiforgetToken or something like that it is called, there may be an error in it (If you haven’t read about it and haven’t done anything with it, then it’s for sure)
You can find more details on the Internet, I gave you the direction)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question