ASP.NET Core MVC 控制器未绑定

发布于 2025-01-14 18:53:31 字数 1031 浏览 3 评论 0原文

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

请求负载显示正在发送值:

network request

我尝试过使用和不使用 [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:

network request

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 技术交流群。

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

发布评论

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

评论(2

清风疏影 2025-01-21 18:53:31

创建一个表示 json 结构的类:

public class Request
{
   public int id {get; set;} 
   string fileName {get; set;} 
}

并在操作中接受它:

public async Task<IActionResult> Edit(Request r)
{
   // use r
}

有关 ASP.NET Core 中模型绑定的更多信息。

Create a class which will represent the json structure:

public class Request
{
   public int id {get; set;} 
   string fileName {get; set;} 
}

And accept it in the action:

public async Task<IActionResult> Edit(Request r)
{
   // use r
}

More on model binding in ASP.NET Core.

魂牵梦绕锁你心扉 2025-01-21 18:53:31

json

public class Request
{
   public int Id {get;set;}
   public string FileName {get;set;}
}

如前面的评论所述,创建一个对象模型来表示控制器操作方法中的

   [HttpPost]
   [Route("items/edit")]
   public IActionResult Edit([FromBody] Request req)
   {
     return Ok();
   }

As stated in previous comment, create an object model to represent the json

public class Request
{
   public int Id {get;set;}
   public string FileName {get;set;}
}

At your controller action method

   [HttpPost]
   [Route("items/edit")]
   public IActionResult Edit([FromBody] Request req)
   {
     return Ok();
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文