Answer the question
In order to leave comments, you need to log in
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);
}
}
[Required]
public Guid UserId { get; set; }
[HttpPost]
[Route("[action]")]
public async Task<IActionResult> Create([FromBody] TimeTrackingDto timeTrackingDto)
{
return Ok();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question