private void InvokeMethod() { if (method != null && this.handlerChildInstance != null && context.AllErrors == null) { Dictionary <string, object> parameters = new Dictionary <string, object>(); var methodParameters = method.GetParameters(); { Dictionary <string, object> values = new Dictionary <string, object>(); if (RequestContent.Trim().IndexOf("{") == 0) { try { values = JsonConvert.DeserializeObject <Dictionary <string, object> >(RequestContent); } catch (Exception e) { throw new Exception("Request deserialization error: " + e.Message + ": " + RequestContent, e); } } //Spin through all of the parameters on the method and see if you can find matches in the request foreach (var methodParam in methodParameters) { //check the post values, query values, route values string value = Form[methodParam.Name].Else(Query[methodParam.Name]).Else(RouteDataToken[methodParam.Name]).Else(RouteValue[methodParam.Name]); //if it's post and there is only one value, try and deseriazlise the whole request content if (value.NOE()) { if (context.Request.HttpMethod.Like("POST") && RequestContent.Trim().IndexOf("{") == 0) { if (methodParameters.Count() == 1) { if (values.ContainsKey(methodParam.Name)) { //there must be a better way that serializeing and deserailizing... value = values[methodParam.Name].GetType() == typeof(String) ? (String)values[methodParam.Name] : values[methodParam.Name].ToJSON(); } else { value = RequestContent; } } else { if (values.ContainsKey(methodParam.Name)) { //there must be a better way that serializeing and deserailizing... value = values[methodParam.Name].GetType() == typeof(String) ? (String)values[methodParam.Name] : values[methodParam.Name].ToJSON(); } } } } if (value.NNOE()) { try { //parse it to it's specific type var newparm = typeof(JSONHelper).GetMethod("ParseJSON").MakeGenericMethod(new Type[] { methodParam.ParameterType }).Invoke(this, new Object[] { value }); parameters.Add(methodParam.Name, newparm); } catch (Exception e) { throw new Exception(methodParam.Name + " is not the right format:" + e.InnerException.Message); } } else if (methodParam.IsOptional) { //add default value parameters.Add(methodParam.Name, methodParam.DefaultValue); } } } //if the number of parameters built up matches the number of parameters in the request then we got a match if (parameters.Count == methodParameters.Length) { //Store output of action "method" var tempdata = method.Invoke(handlerChildInstance, parameters.Count == 0 ? new object[] { } : parameters.Select(x => x.Value).ToArray()); if (data == null && tempdata != null) { this.data = tempdata; } } else //thow some errors { var missingparams = methodParameters.Where(x => !x.Name.LikeOne(parameters.Select(y => y.Key).ToArray())).Select(x => x.ParameterType.ToString() + " " + x.Name).ToArray(); sb.Required = methodParameters.Where(x => !x.Name.LikeOne(parameters.Select(y => y.Key).ToArray())).Select(x => x.Name).ToArray(); if (AcceptType == ContentType.JSON.Description() || AcceptType == ContentType.JSONP.Description()) { sb.ErrorMsg = "Missing " + (methodParameters.Length - parameters.Count) + " required fields " + (context.IsDebuggingEnabled ? String.Join(", ", sb.Required) : ""); } else { throw new Exception("Missing " + (methodParameters.Length - parameters.Count) + " parameters needed to invoke this method via " + AcceptType + ": \n\n" + String.Join("\n", missingparams) + "\n\n"); } } } }