Reputation: 155
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; }
}
This is my API. My problem is the index_fields is a collection of name value pairs, which is optional and variable length. The thing is I don't know the names that will be passed to my GET method ahead of time. An example call is:
/api/workitems?where_name=workitem&foo=baz&bar=yo
Is IModelBinder the way to go here, or is there a simpler approach? If it's IModelBinder how do I iterate through the names? I went here to see an example of IModelBinder: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api But I don't see a way to iterate through the names and pick out "foo" and "bar".
I tried changing index_fields to Dictionary<string, string>
and no IModelBinding but that did nothing: index_fields was null. When I'm doing IModelBinder and debugging my IModelBinder.BindModel routine, if I drill down into the ModelBindingContext object, I can see the "foo" and "bar" values in the System.Web.Http.ValueProviders.Providers.QueryStringValueProvider
, but I don't know how to use that. I tried creating a QueryStringValueProvider from scratch but it takes an HttpActionContext. Again, I don't see a way to iterate through the keys to get "foo" and "bar".
BTW: I'm using VS2012
Upvotes: 0
Views: 3907
Reputation: 5442
You can simply iterate through the query parameters
public ActionResult Method()
{
foreach(string key in Request.QueryString)
{
var value = Request.QueryString[key];
}
}
Upvotes: 13