记录 POST 请求正文中的模型数据

发布于 2025-01-13 03:29:23 字数 1006 浏览 0 评论 0原文

我的要求是记录我从 POST 请求接收到的模型数据。但是当我尝试从 ModelState.Values 获取值时,我没有收到任何内容。我也尝试记录请求正文,但与请求正文相同,我没有收到任何数据。

    [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);
        }

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);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2025-01-20 03:29:23

当我们收到请求正文中包含数据的 post 请求时,我认为我们无法从 ModelState.Values 获取值,您可能会看到下面的屏幕截图,ModelState.Values > 不包含数据,我们需要从模型本身获取数据。

输入图片此处描述
输入图片这里的描述

另一个例子,请看我下面的屏幕截图,当我向我的api发送这样的post请求时,我可以在我的模型中看到数据,但在请求正文或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;
}

在此处输入图像描述

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.

enter image description here
enter image description here

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;
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文