错误:操作具有与请求正文绑定的多个参数 [英] Error: Action has more than one parameter bound from request body

查看:1451
本文介绍了错误:操作具有与请求正文绑定的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.Net MVC项目的控制器中编写了一个新方法,并在下面出现错误.我认为InvalidOperationException来自Swagger.我将其标记为"ignored Api",希望它会跳过该方法,但错误仍然存​​在:

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:操作'Gorilla.WebApi.Source.Controller.Campaigns.BeverageCapacityCampaignController.CalculatePriceWithCampaign(Gorilla.WebApi)'具有多个从请求正文中指定或推断出的参数.每个动作只能绑定一个参数.检查以下参数,并使用'FromQueryAttribute'指定来自查询的绑定,'FromRouteAttribute'指定来自路由的绑定,以及'FromBodyAttribute'指定从body绑定的参数:
BeverageCapacityCampaign广告系列
饮料容量

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

我可以找到建议检查nuget的信息,但是我所有的Nu​​get都是最新的.

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

推荐答案

该错误来自模型绑定,与Swagger无关(ApiExplorerSettings属性的存在对错误没有影响).

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 

模型绑定默认从请求主体绑定复杂参数.但是,每个动作只能绑定一个参数.

所以您需要

  1. 将它们组合到一个只包装/保存两个参数的类中 作为属性-并使其与主体(作为一个对象)绑定
  2. 确定要与身体绑定的对象以及与之绑定的对象 路由或查询,然后将属性[FromRoute]或[FromQuery]添加到一个,并将[FromBody]添加到另一个.
  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.

System.Web.Http.Description中的

ApiExplorerSettings将忽略帮助页面中的归因操作,或其他任何原因(可能是大摇大摆)...但是您仍然会收到此异常-来自模型绑定级别的问题

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

这篇关于错误:操作具有与请求正文绑定的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆