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

前端 未结 6 1076
死守一世寂寞
死守一世寂寞 2021-01-11 14:15

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

相关标签:
6条回答
  • 2021-01-11 14:18

    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.

    0 讨论(0)
  • 2021-01-11 14:27

    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

    0 讨论(0)
  • 2021-01-11 14:33

    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
    
    0 讨论(0)
  • 2021-01-11 14:33

    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.

    0 讨论(0)
  • 2021-01-11 14:35

    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**
    
    0 讨论(0)
  • 2021-01-11 14:40

    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

    0 讨论(0)
提交回复
热议问题