简体   繁体   English

如何在 IActionResult Asp.net WebAPI 中传递多个参数(对象)

[英]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.

相关问题 asp.net webapi:如何传递可选参数? - asp.net webapi: how to pass optional parameters? 如何在 IActionResult 方法中传递多个对象 - How to pass multiple objects in IActionResult method 如何在asp.net核心webapi项目中如何将多个参数传递给控制器​​,其中一个参数是文件或字节[] - How in asp.net core webapi project you can pass multiple parameters to a controller where one of them is file or a byte[] 如何在asp.net WEB API中传递多个querystring参数 - how to pass multiple querystring parameters in asp.net WEB API ASP.NET Core中如何将多个参数传递给一个get方法 - How to pass multiple parameters to a get method in ASP.NET Core 如何在asp.net中使用Querystring传递多个参数? - How to pass multiple parameters using Querystring in asp.net? 如何将此IHttpActionResult转换为ASP.NET Core IActionResult - How to translate this IHttpActionResult to ASP.NET Core IActionResult 如何在 ASP.NET Core 中返回 403 Forbidden 响应作为 IActionResult - How to return 403 Forbidden response as IActionResult in ASP.NET Core 如何将HTML参数传递给ASP.NET? - How to pass HTML parameters to ASP.NET? ASP.NET WebAPI 2:如何将空字符串作为 URI 中的参数传递 - ASP.NET WebAPI 2: How to pass empty string as parameter in URI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM