asp.net-web-api – WebApi – 传递一组值
我需要使用ASP.NET Web API(版本4.5.2)构建API.首先,我只想创建一个添加一些数字的基本端点.为了做到这一点,我创建了:
[RoutePrefix("api/test")] public class MyController : ApiController { [HttpGet] public IEnumerable<int> Calulate(decimal[] op1,decimal[] op2) { var results = new List<Calculation>(); for (var i=0; i<op1.Length; i++) { var calculation = new Calculation(); calculation.Operand1 = op1[i]; calculation.Operand2 = op2[i]; calculation.Calculate(); results.Add(calculation); } return results; } public class Calculation { public int Operand1 { get; set; } public int Operand2 { get; set; } public int Result { get; set; } public void Calculate() { this.Result = this.Operand1 + this.Operand2; } } } 我现在正试图通过Postman Chrome应用程序点击此端点.当我通过Postman运行它时,我收到了一个错误.这是我正在做的事情: 在Postman中,我将“http://localhost:50668/api/test/calculate”放在“GET”下拉列表旁边的URL字段中.然后我点击“发送”.我收到以下错误: { "Message": "An error has occurred.","ExceptionMessage": "Can't bind multiple parameters ('op1' and 'op2') to the request's content.","ExceptionType": "System.InvalidOperationException","StackTrace": "..." } 我认为(我不知道)原因是因为我没有正确地将值传递给Postman的API.但是,我不知道该怎么做.如何将值数组传递给API? 解决方法
简短的回答
要发送小数组数组,WebApi期望url签名如下: 该url将发送[1.0,2.0]作为Operand1和[3.0,4.0]作为Operand2. 答案很长 通过使用GET http://localhost:50668/api/test/calculate调用您的api,您实际上不会向您的服务器发送任何内容. (除标题内容外) 如果要将数据发送到服务器,则至少有2个选项: 选项2:如果操作是幂等的,则使用GET方法 [HttpGet] [Route("calculate")] public List<Calculation> CalculateWithGet([FromUri]decimal[] Operand1,[FromUri]decimal[] Operand2) { var results = new List<Calculation>(); for (var i = 0; i < Operand1.Length; i++) { var calculation = new Calculation(); calculation.Operand1 = Operand1[i]; calculation.Operand2 = Operand2[i]; calculation.Calculate(); results.Add(calculation); } return results; } public class Calculation { public decimal Operand1 { get; set; } public decimal Operand2 { get; set; } public decimal Result { get; set; } public void Calculate() { Result = this.Operand1 + this.Operand2; } } 使用REST客户端,它应该如下所示: 使用GET,数据通过URL发送 请注意,如果您使用GET方法,服务器将期望从URL接收输入.因此,您应该发送以下查询: 如果操作不是幂等的,请使用POST方法 由于操作进行了一些服务器端计算,我假装它可能并不总是幂等的.如果是这种情况,POST可能更合适. [HttpPost] [Route("calculate")] public List<Calculation> CalculateWithPost(CalculationInputs inputs) { var results = new List<Calculation>(); for (var i = 0; i < inputs.Operand2.Length; i++) { var calculation = new Calculation(); calculation.Operand1 = inputs.Operand1[i]; calculation.Operand2 = inputs.Operand2[i]; calculation.Calculate(); results.Add(calculation); } return results; } public class CalculationInputs { public decimal[] Operand1 { get; set; } public decimal[] Operand2 { get; set; } } public class Calculation { public decimal Operand1 { get; set; } public decimal Operand2 { get; set; } public decimal Result { get; set; } public void Calculate() { Result = this.Operand1 + this.Operand2; } } 使用POST,数据通过正文发送 使用该结构,服务器期望从请求主体接收输入.如果正文与函数的签名匹配,WebApi将反序列化正文. 使用REST客户端,它应该如下所示: 边注 用于获取SwaggerUI生成的nuget包(printscreens)可以找到here.在WebApis上运行adhoc测试非常有用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – MasterPage是否知道正在显示的页面?
- asp.net-web-api-405使用AttributeRouting.PUTAttribute,除
- asp.net-core – 使用ASP.NET Core从`project.json`中排除发
- asp.net-mvc – Web API,OData,$inlinecount和测试
- asp.net-mvc – ASP.NET MVC快速教程
- asp.net-mvc – 从azure云存储下载大文件的最佳方式
- asp.net-mvc – 在asp.net mvc控制器中使用构造函数注入的I
- asp.net – 是否有一个简单的方法来呈现具有Microsoft Web
- asp.net – 如何停止所有cassini实例?
- asp.net-mvc – 域vs DTO vs ViewModel – 如何和何时使用它
- asp.net-mvc – 在ASP.NET MVC中生成链接?
- asp.net – 如何在IIS上配置Web部署发布功能,以便
- asp.net-mvc – 更新实体框架MVC中的子实体
- asp.net-mvc – 修改模型时更新视图
- asp.net – Application_Start和Application_OnS
- asp.net-mvc – 使用Angular VS Razor进行ASP.Ne
- asp.net-mvc – 来自父Razor布局的@Functions继承
- asp.net-mvc – 应用偏移量时表示的UTC时间必须介
- asp.net-mvc – VaryByParam =“*”是否也读取了
- asp.net-mvc-2 – 如何格式化内联剃刀变量