错误:操作具有与请求正文绑定的多个参数

编程入门 行业动态 更新时间:2024-10-17 02:57:44
本文介绍了错误:操作具有与请求正文绑定的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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

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

所以您需要

  • 将它们组合到一个只包装/保存两个参数的类中 作为属性-并使其与主体(作为一个对象)绑定
  • 确定要与身体绑定的对象以及与之绑定的对象 路由或查询,然后将属性[FromRoute]或[FromQuery]添加到一个,并将[FromBody]添加到另一个.
  • Combine them into one class that just wraps / holds both parameters as properties - and have them bound from the body (as one object)
  • 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

    更多推荐

    错误:操作具有与请求正文绑定的多个参数

    本文发布于:2023-11-02 17:02:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1552935.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   绑定   错误   参数   操作

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!