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.
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.
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)
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)
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)
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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With