我在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的信息,但是我所有的Nuget都是最新的.
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模型绑定的默认是从请求主体绑定复杂参数.但是,每个动作只能绑定一个参数.
所以您需要
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
更多推荐
错误:操作具有与请求正文绑定的多个参数
发布评论