private CollectionsQueryModelParameterDetails GetDetailsForActionParameter(ActionParameter actionParam) { string key = nameof(CollectionsQueryModelParameterDetails); var value = actionParam.GetAdditionalDataOrDefault <CollectionsQueryModelParameterDetails>(key); if (value == null) { value = new CollectionsQueryModelParameterDetails(actionParam.Type); actionParam.SetAdditionalData(key, value); } return(value); }
/// <summary> /// Parses query parameter /// </summary> /// <param name="request">HTTP request from which to retrieve value(s).</param> /// <param name="actionCtx">Action context of the parameter.</param> /// <param name="parameter">The parameter to parse.</param> /// <returns> /// object, Value of the parameter /// </returns> /// <exception cref="System.NotImplementedException"></exception> public override object ParseParameterValue(HttpRequest request, ActionContext actionCtx, ActionParameter parameter) { CollectionsQueryModelParameterDetails details = GetDetailsForActionParameter(parameter); string key = null; IEnumerable <KeyValuePair <string, StringValues> > source = null; if (parameter.ParameterSource == ParameterSources.Query) { source = request.Query; } else if (parameter.ParameterSource == ParameterSources.Header) { source = request.Headers; } if (source != null) { key = source.FirstOrDefault(x => x.Key.ToLower() == parameter.Name).Key; } string[] values = new string[0]; if (key != null) { values = source.FirstOrDefault(x => x.Key == key).Value; if (values == null) { values = new string[0]; } } if (details.IsArray || details.IsIEnumerable) { Array a = Array.CreateInstance(details.OriginalCollectionElementType, values.Length); for (int i = 0; i < values.Length; i++) { a.SetValue(ParseSingleQueryValue(values[i], details.CollectionElementType, details.IsCollectionElementTypeNullable, parameter.Name, new Lazy <string>(() => parameter.ParentActionContext.ToString())), i); } return(a); } if (details.IsList) { IList parsedValues = Activator.CreateInstance(parameter.Type) as IList; foreach (var value in values) { parsedValues.Add(ParseSingleQueryValue(value, details.CollectionElementType, details.IsCollectionElementTypeNullable, parameter.Name, new Lazy <string>(() => parameter.ParentActionContext.ToString()))); } return(parsedValues); } throw new Exception($"Parameter {parameter} in action {actionCtx} is not array, list or IEnumerable."); }