V
V
Victor P.2021-07-27 23:14:45
ASP.NET
Victor P., 2021-07-27 23:14:45

Why does the DataAnnotation validation work up to the body of the controller and return the result immediately?

Hello to all fans of si lattice.
I use .net core version 3.0, an empty mvc project
I write a method in the controller to create some kind of entity:

[HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> Create([FromBody] TimeTrackingDto timeTrackingDto)
        {
            timeTrackingDto.UserId = HttpContext.User.GetUserId();

            if (ModelState.IsValid)
            {
                var id = await service.Create(timeTrackingDto);
                return Ok(id);
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

The model has a UserId field marked with an attribute, for example,
[Required]
  public Guid UserId { get; set; }

I planned to fill this field with the current user in the Create method (before transferring control to the service that writes to the database), and in the Update method the field should be filled anyway, so I want to leave one model for both creating and updating the entity.

The crux of the matter is what. The control does not enter the Create method. That is, if I put a breakpoint on the first line there and run it in debugger mode, I will never get into this method. Always returns a 400 error with a ModelState body and errors. Even if I write a method like this:
[HttpPost]
  [Route("[action]")]
  public async Task<IActionResult> Create([FromBody] TimeTrackingDto timeTrackingDto)
  {
    return Ok();
  }

The response will still be 400 with a ModelState body.
If I remove all DataAnnotation attributes, then the control will easily enter the method.

As far as I understand, this is incorrect behavior. Even if an incorrect (not valid) model came to the server, I want to manually set some fields and then manually check ModelState.IsValid. It seemed to me that earlier (I had not followed this for a long time, in the .net framework and even in the .net core of early versions) there was just such a behavior.

In a startup, there are no overrides for this behavior.
Everything I google leads to primitive articles on how this validation works and not a word about this behavior and how to deal with it.
Of course, I can make correct behavior with crutches, this is not to offer.
I want to understand this issue, please help me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2021-07-28
@Jeer

Judging by the code, you have an Api controller.


Web API controllers do not need to check ModelState.IsValid when the [ApiController] attribute is present. In this case, an HTTP 400 response is automatically returned with error details if the model state is not valid. For more information, see HTTP 400 Automatic Responses.
https://docs.microsoft.com/en-us/aspnet/core/mvc/m...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question