Reputation: 161
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);
}
}
Upvotes: 2
Views: 5835
Reputation: 419
I was getting this error on a .NET 7 web Api controller being migrated from .NET 4.8. In the original controller there was a public void method consumed by the actions of the controller. The error was occurring in Program.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: default,
pattern: "{controller=Home}/{action=Index}/{id?}");
});
I changed the method to private void which fixed it. I don't know why routing was trying to include the method as an endpoint as there wasn't a routing attribute.
Upvotes: 0
Reputation: 18209
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":
{
...
}
}
Upvotes: 1