'Logging the model data from the POST request body
My requirement is to log the model data that I am receiving from a POST request. but when I am tring to get the values from ModelState.Values, I am not receiving anything. I also tried to Log Request body, but same with the request body also, I am not receiving any data.
[Route("")]
[HttpPost]
public IActionResult Send([FromBody]RequestDto model)
{
return CreateResponse(() => _sendTask.Send(model, false));
}
[ApiController]
public class ApiController : ControllerBase
{
#region Protected methods
protected IActionResult CreateResponse()
{
try
{
if (ModelState.IsValid)
{
StringBuilder value = new StringBuilder();
foreach (ModelStateEntry values in ModelState.Values)
{
value.Append(values);
}
return BadRequest(value.ToString());
}
catch (Exception ex)
{
Logger.LogCritical("Exception occurred during processing of a request", ex);
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
Solution 1:[1]
When we received a post request which contained data in request body, I don't think we can get the value from ModelState.Values, you may see screenshot below, ModelState.Values doesn't contain data, we need to get data from the Model itself.
Another example, see my following screenshot, when I send such post request to my api, I can see data in my model but no data in request body or modelstate.value
[HttpPost]
public void getModel([FromBody] Movie model) {
var request = HttpContext.Request.Body;
if (ModelState.IsValid){
var value = ModelState.Values;
}
var a = model.Price;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



