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
I got this error by inheriting from BaseController instead of ControllerBase. It was a class from another library that is completely unrelated and I misremembered the name. The exception was a red herring for me.
My controller has some refactored code whose methods are marked public. Looks like either moving them out of the controller or marking private corrects this problem. Or attributing the pesky methods with [NonAction]
might also be a choice as asked at asp.net Core mvc hide and exclude Web Api Controller Method
For me in the definition of the a new controller automatically add this prerequisite.
I removed it and it works
[Route("api/[controller]")]
[Apicontroller] //remove this line
Another possible solution is to nest the complex data types in a tuple:
[ApiExplorerSettings(IgnoreApi = true)]
public decimal CalculatePriceWithCampaign((BeverageCapacityCampaign campaign, BeverageCapacity capacity) data, int count = 1)
{
switch (data.campaign.DiscountType)
{
case DiscountType.Fixed:
return (data.capacity.CapacityPrice - data.campaign.DiscountValue) * count;
case DiscountType.Percentage:
return (data.capacity.CapacityPrice * count) * data.campaign.DiscountValue;
default:
return data.capacity.CapacityPrice;
}
}
However, NSwag (Swagger) does not seems to be able to automatically parse this case because a non-valid example gets generated. NSwagStudio recognizes the case correctly and generates valid client code.
I received error "has more than one parameter that was specified ..." for having mention of [ApiController] on top of class and then inheriting class from APIController.
Corrected issue by inheriting the class from Controller.
[Authorize]
[Route("api/the")]
**[ApiController]**
public class TheController : **Controller**
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