的ASP.NET Web API - 获取与多个参数的要求 [英] ASP.NET Web API - GET request with multiple arguments

查看:604
本文介绍了的ASP.NET Web API - 获取与多个参数的要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的是有一个像API请求/api/calculator?1=7.00&2=9.99&3=5.50&4=45.76等我如何控制器可抢请求数据?钥匙/ $ C $查询字符串的CS是1和1000之间的整数。在查询字符串他们可以部分的1000 codeS,不一定所有的人即可。该值的部分是双打。

What I'm trying to do is to have an api request like /api/calculator?1=7.00&2=9.99&3=5.50&4=45.76 etc. How my controller can grab the request data? The keys/codes of the query string are ints between 1 and 1000. In the query string they can be some of the 1000 codes, not necessarily all of them. The value part is doubles.

单程我想会的工作是,如果我创建了一个模型对象(例如 StupidObject )1000的属性(应该使用命名像P1,P2,.. P1000性能现在codeS,为int的数据是不是允许的属性名),饰以 ModelBinder的。那么对于控制器我可以像 GetCalcResult(StupidObject OBJ){...} 但是,这似乎并不像一个优雅的解决方案:)

The one way I think would work is if I create a model object (ex StupidObject) with 1000 properties (should use properties named like p1, p2,..p1000 for codes now, as ints are not an allowed property name), decorated with ModelBinder. Then for the controller I could have something like GetCalcResult(StupidObject obj){...} But that doesn't seem like an elegant solution :)

我试着像 GetCalcResult控制器([FromURI]字典< INT,双>快译通){...} ,但字典总是空。也没有 [FromURI] 我得到一个错误。也试过列表< KeyValuePair< INT,双>方式> 与相同的结果控制器参数

I tried controllers like GetCalcResult([FromURI]Dictionary<int, double> dict){...} but dict is always null. Also without the [FromURI] I get an error. Also tried List<KeyValuePair<int, double>> as controller parameter with the same results.

任何人都可以点我在正确的方向,或给我一个工作的例子?

Could anyone point me at the right direction or give me an working example?

推荐答案

的一种方法是,以避免试图通过作为一个参数的值,只是做

One way is to avoid trying to pass the values in as a parameter and simply do

  var queryValues = Request.RequestUri.ParseQueryString();

在你的操作方法。 QueryValues​​将NameValueCollection中,你可以遍历到你的查询参数进行访问。

in your action method. QueryValues will be NameValueCollection that you can iterate over to get access to your query parameters.

如果你真的想用一个参数,其类型的参数[FromUri] FormDataCollection可能会奏效。

If you really want to use a parameter, having a parameter of type [FromUri]FormDataCollection might work.

这篇关于的ASP.NET Web API - 获取与多个参数的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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