ASP.NET Core MVC 控制器未绑定
从 fetch api 向端点发出请求,并且 props 不会绑定。
端点上的 id 和 fileName 分别为 0 和 null。
我的获取:
fetch(`https://localhost:44343/items/edit`, {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id:123,
fileName:"asd"
})
})
.then(response => console.log(response))
.catch(error => console.error('Unable to update item.', error));
我的端点:
[HttpPost]
public async Task<IActionResult> Edit([FromBody]int id, string fileName)
{
return Ok(id);
}
请求负载显示正在发送值:
我尝试过使用和不使用 [FromBody]
显式添加操作的路由,更改为 PUT 而不是POST(为什么更新操作的默认设置是 POST??)
我还可以尝试什么?
Doing a request from the fetch api to the endpoint and the props do not bind.
The id and fileName are 0 and null respectively on the endpoint.
My fetch:
fetch(`https://localhost:44343/items/edit`, {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id:123,
fileName:"asd"
})
})
.then(response => console.log(response))
.catch(error => console.error('Unable to update item.', error));
My endpoint:
[HttpPost]
public async Task<IActionResult> Edit([FromBody]int id, string fileName)
{
return Ok(id);
}
The request payload shows that the values are being sent:
I've tried using and not using the [FromBody]
adding explicitly the route to the action, changing to PUT instead of POST (why the heck does the default for the update action is a POST??)
Anything else I can try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个表示 json 结构的类:
并在操作中接受它:
有关 ASP.NET Core 中模型绑定的更多信息。
Create a class which will represent the json structure:
And accept it in the action:
More on model binding in ASP.NET Core.
json
如前面的评论所述,创建一个对象模型来表示控制器操作方法中的
As stated in previous comment, create an object model to represent the json
At your controller action method