英文:
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<IActionResult> Post(model dto)
{
.......
return View("~/Views/Home/Index.cshtml", 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<IActionResult> Post([FromBody]model dto)
{
.......
return View("~/Views/Home/Index.cshtml", mainViewModel);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论