private static bool IsTypeBindable(Type parameterInfo) { if (Binder.IsCollection(parameterInfo)) { return(true); } return(parameterInfo.Namespace != null && !parameterInfo.Namespace.StartsWith("System", StringComparison.Ordinal)); }
public IEnumerable <object> BuildCallingArguments(RequestContext context, IApiMethodCall methodToCall) { var callArg = new List <object>(); var requestParams = GetRequestParams(context); var methodParams = methodToCall.GetParams().Where(x => !x.IsRetval).OrderBy(x => x.Position); foreach (var parameterInfo in methodParams) { if (requestParams[parameterInfo.Name] != null) { //convert var values = requestParams.GetValues(parameterInfo.Name); if (values != null && values.Any()) { if (Binder.IsCollection(parameterInfo.ParameterType)) { callArg.Add(Binder.Bind(parameterInfo.ParameterType, requestParams, parameterInfo.Name)); continue; //Go to next loop } try { callArg.Add(ConvertUtils.GetConverted(values.First(), parameterInfo)); //NOTE; Get first value! } catch (ApiArgumentMismatchException) { //Failed to convert. Try bind callArg.Add(Binder.Bind(parameterInfo.ParameterType, requestParams, parameterInfo.Name)); } } } else { //try get request param first. It may be form\url-encoded if (!"GET".Equals(context.HttpContext.Request.HttpMethod, StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(context.HttpContext.Request[parameterInfo.Name])) { //Drop to callArg.Add(ConvertUtils.GetConverted(context.HttpContext.Request[parameterInfo.Name], parameterInfo)); } else if (parameterInfo.ParameterType == typeof(ContentType) && !string.IsNullOrEmpty(context.HttpContext.Request.ContentType)) { callArg.Add(new ContentType(context.HttpContext.Request.ContentType)); } else if (parameterInfo.ParameterType == typeof(ContentDisposition) && !string.IsNullOrEmpty(context.HttpContext.Request.Headers["Content-Disposition"])) { var disposition = new ContentDisposition(context.HttpContext.Request.Headers["Content-Disposition"]); disposition.FileName = HttpUtility.UrlDecode(disposition.FileName); //Decode uri name callArg.Add(disposition); } else if (parameterInfo.ParameterType.IsSubclassOf(typeof(HttpPostedFile)) && context.HttpContext.Request.Files[parameterInfo.Name] != null) { callArg.Add(context.HttpContext.Request.Files[parameterInfo.Name]); } else if (Binder.IsCollection(parameterInfo.ParameterType) && parameterInfo.ParameterType.IsGenericType && parameterInfo.ParameterType.GetGenericArguments().First() == typeof(HttpPostedFileBase)) { //File catcher var files = new List <HttpPostedFileBase>(context.HttpContext.Request.Files.Count); files.AddRange(from string key in context.HttpContext.Request.Files select context.HttpContext.Request.Files[key]); callArg.Add(files); } else { if (parameterInfo.ParameterType.IsSubclassOf(typeof(Stream)) || parameterInfo.ParameterType == typeof(Stream)) { //First try get files var file = context.HttpContext.Request.Files[parameterInfo.Name]; callArg.Add(file != null ? file.InputStream : context.HttpContext.Request.InputStream); } else { //Try bind //Note: binding moved here if (IsTypeBindable(parameterInfo.ParameterType)) { //Custom type var binded = Binder.Bind(parameterInfo.ParameterType, requestParams, parameterInfo.Name); if (binded != null) { callArg.Add(binded); continue; //Go to next loop } } //Create null var obj = parameterInfo.ParameterType.IsValueType ? Activator.CreateInstance(parameterInfo.ParameterType) : null; callArg.Add(obj); } } } } return(callArg); }