简体   繁体   中英

Action has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body

Error : Action has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body .netcore

When i wrote a new httpPost method with 2 parameters of my .net core project and getting above error. how can i solve this.

        [HttpPost]
        public async Task<IActionResult> Create([FromBody] UserBO userBO, [FromBody] SiteCode siteCode)
        {
            try
            {

                await _userService.CreateUserAsync(userBO, siteCode);

                return Created(nameof(Get), userBO);
            }
            catch (Exception ex)
            {
                return HandleException(ex);
            }
        }

You can try to create a new Model which contains userBO and siteCode:

Model:

public class USModel
    {
        public UserBO userBO { get; set; }
        public SiteCode siteCode { get; set; }
    }

Action:

[HttpPost]
        public async Task<IActionResult> Create([FromBody] USModel uSModel)
        {
            try
            {

                await _userService.CreateUserAsync(uSModel.userBO, uSModel.siteCode);

                return Created(nameof(Get), uSModel.userBO);
            }
            catch (Exception ex)
            {
                return HandleException(ex);
            }
        }

json format:

{
    "userBO":
    {
        ...
    },
    "siteCode":
    {
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM