Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Action has more than one parameter bound from request body

Tags:

c#

asp.net-mvc

I wrote a new method into my Controller of my ASP.Net MVC project and getting error below. I think InvalidOperationException coming from with Swagger. I marked it as "ignored Api" hoping it will skip the method but error still there:

[ApiExplorerSettings(IgnoreApi = true)]
public decimal CalculatePriceWithCampaign(
       BeverageCapacityCampaign campaign, 
       BeverageCapacity capacity,
       int count = 1)
{
    switch (campaign.DiscountType)
    {
        case DiscountType.Fixed:
            return (capacity.CapacityPrice - campaign.DiscountValue) * count;
        case DiscountType.Percentage:
            return (capacity.CapacityPrice * count) * campaign.DiscountValue;
        default:
            return capacity.CapacityPrice;
    }
}

But when running I am getting this error:

An unhandled exception occurred while processing the request.

InvalidOperationException: Action 'Gorilla.WebApi.Source.Controller.Campaigns.BeverageCapacityCampaignController.CalculatePriceWithCampaign (Gorilla.WebApi)' 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. Inspect the following parameters, and use 'FromQueryAttribute' to specify bound from query, 'FromRouteAttribute' to specify bound from route, and 'FromBodyAttribute' for parameters to be bound from body:
BeverageCapacityCampaign campaign
BeverageCapacity capacity

Information I could find suggested to check nugets, but all my Nugets are up-to-date.

like image 878
DirtyNative Avatar asked Dec 19 '18 15:12

DirtyNative


People also ask

Can we declare FromBody attribute on multiple parameters?

The [FromBody] attribute can be applied on only one primitive parameter of an action method. It cannot be applied to multiple primitive parameters of the same action method.

How to bind multiple parameters from the body of a request?

The default for Model Binding is to bind complex parameters from the body of the request. However, only one parameter per action may be bound from body. So you need to either Combine them into one class that just wraps / holds both parameters as properties - and have them bound from the body (as one object)

How to bind multiple parameters to one object in a class?

The default for Model Binding is to bind complex parameters from the body of the request. However, only one parameter per action may be bound from body. Combine them into one class that just wraps / holds both parameters as properties - and have them bound from the body (as one object)

Is it possible to have two parameters from the body?

However, only one parameter per action may be bound from body. Combine them into one class that just wraps / holds both parameters as properties - and have them bound from the body (as one object)

How to set parameters to be bound from the body?

Only one parameter per action may be bound from body. Inspect the following parameters, and use 'FromQueryAttribute' to specify bound from query, 'FromRouteAttribute' to specify bound from route, and 'FromBodyAttribute' for parameters to be bound from body:


1 Answers

The error is coming from model binding and is not related to Swagger (the presence of ApiExplorerSettings attribute has no impact on error).

You have two complex parameters. i.e. of Complex types

BeverageCapacityCampaign 
BeverageCapacity 

The default for Model Binding is to bind complex parameters from the body of the request. However, only one parameter per action may be bound from body.

So you need to either

  1. Combine them into one class that just wraps / holds both parameters as properties - and have them bound from the body (as one object)
  2. Decide which to bind from the body, and which from the route or the query and add the attributes [FromRoute] or [FromQuery] to one, and [FromBody] to the other.

ApiExplorerSettings from System.Web.Http.Description will ignore the attributed action from a help page, or whatever else (maybe swagger)... but you will still get this exception - from problems at level of Model Binding

like image 81
MemeDeveloper Avatar answered Sep 20 '22 13:09

MemeDeveloper