Post API:在RESTful API中,输入方法为空。

huangapple go评论57阅读模式
英文:

Post API : Input methods are empty in restful API

问题

当我将content-type设置为表单时,API的POST方法会发送带有值的输入,但当我将content-type设置为JSON时,它会发送所有输入都为null。
我不知道为什么会这样发生?
这是我的代码:

[HttpPost]
public async Task<IActionResult> Post(model dto)
{
    .......
    return View("~/Views/Home/Index.cshtml", mainViewModel);
}

在代码的某个地方是否有可能默认的content-type被更改了?

英文:

I have an API post method. When I set content-type as form, it sends inputs with value but when I set the content-type as JSON it sends all of them null.
I don't know how it is happening?
This is my code:

[HttpPost]
public async Task&lt;IActionResult&gt; Post(model dto)
{
    .......
    return View(&quot;~/Views/Home/Index.cshtml&quot;, mainViewModel);
}

Is it possible in somewhere in the code default content-type got changed?

答案1

得分: 1

尝试:

[HttpPost]
public async Task<IActionResult> Post([FromBody]model dto)
{
    .......
    return View("~/Views/Home/Index.cshtml", mainViewModel);
}
英文:

> when i set the content-type as json

Try

[HttpPost]
    public async Task&lt;IActionResult&gt; Post([FromBody]model dto)
    {
        .......
        return View(&quot;~/Views/Home/Index.cshtml&quot;, mainViewModel);
    }

huangapple
  • 本文由 发表于 2023年7月24日 15:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定