config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
public HttpResponseMessage Get( string where_name,
IndexFieldsModel index_fields = null )
public class IndexFieldsModel
{
public List<IndexFieldModel> Fields { get; set; }
}
public class IndexFieldModel
{
public string Name { get; set; }
public string Value { get; set; }
}
这是我的API。我的问题是index_fields是名称值对的集合,这是可选的并且是可变长度的。问题是我不知道将要提前传递给我的GET方法的名称。一个示例调用是:
/api/workitems?where_name=workitem&foo=baz&bar=yo
是IModelBinder还是这里更简单的方法?如果是IModelBinder,如何遍历名称?我在这里看到了IModelBinder的示例:http : //www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api但是我没有看到一种遍历名称并选择“ foo”和“ bar”的方法。
我尝试将index_fields更改为Dictionary<string, string>
IModelBinding,但没有执行任何操作:index_fields为null。在执行IModelBinder并调试IModelBinder.BindModel例程时,如果我深入到ModelBindingContext对象,则可以在中看到“ foo”和“ bar”值System.Web.Http.ValueProviders.Providers.QueryStringValueProvider
,但是我不知道如何使用它。我尝试从头开始创建QueryStringValueProvider,但是它需要HttpActionContext。再说一次,我没有找到遍历键来获取“ foo”和“ bar”的方法。
顺便说一句:我正在使用VS2012
您可以简单地遍历查询参数
public ActionResult Method()
{
foreach(string key in Request.QueryString)
{
var value = Request.QueryString[key];
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句