netcore http post 可以多个 frombody
时间: 2023-08-10 22:08:09 浏览: 83
是的,ASP.NET Core的HTTP POST操作可以使用多个FromBody参数。但是,每个FromBody参数只能绑定一个请求正文,并且只能有一个请求正文。
如果您需要同时传递多个对象,可以考虑将它们包装在一个父对象中,并将该对象传递为单个FromBody参数。
以下是使用多个FromBody参数的示例:
```
[HttpPost]
public IActionResult Create([FromBody]User user, [FromBody]Address address)
{
// code to create user and address
}
```
在此示例中,我们尝试从请求正文中绑定User和Address对象。但是,由于每个FromBody参数只能绑定一个请求正文,这将不起作用。
因此,我们需要将User和Address对象封装在一个父对象中,如下所示:
```
public class CreateUserModel
{
public User User { get; set; }
public Address Address { get; set; }
}
[HttpPost]
public IActionResult Create([FromBody]CreateUserModel createUserModel)
{
var user = createUserModel.User;
var address = createUserModel.Address;
// code to create user and address
}
```
现在,我们可以将CreateUserModel对象作为单个FromBody参数传递,并从中获取User和Address对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)