[英]How to pass multiple parameters (Objects) in IActionResult Asp.net WebAPI
[HttpPost("addtocart")]
public IActionResult AddToCart( Cart cart,
Product product,
ProductOption productOption
)
{
_cartService.Add(cart);
_productService.Add(product);
_productOptionService.Add(productOption);
return NoContent();
}
Why I cannot use IActionResult with multiple parameters and how to solve this issue为什么我不能使用带有多个参数的 IActionResult 以及如何解决这个问题
If you want to post all of those in 1 operation, you need to wrap them in some object.如果您想在 1 次操作中发布所有这些,则需要将它们包装在一些 object 中。
public class Something
{
public Cart Cart {get;set;}
public Product Product {get;set;}
public ProductOption ProductOption {get;set;}
}
and then take that in from the body of your request然后从您的请求正文中获取
[HttpPost("addtocart")]
public IActionResult AddToCart([FromBody] Something something)
{
_cartService.Add(something.Cart);
_productService.Add(something.Product);
_productOptionService.Add(something.ProductOption);
return NoContent();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.