Exemple #1
0
 public void RegisterController(Controller controller)
 {
     if (Controllers.IndexOf(controller) == -1)
         Controllers.Add(controller);
 }
Exemple #2
0
        MethodInfo ResolveAction(Controller controller, IRequest request, Dictionary<string, string> values, out object[] actionParameters)
        {
            // TODO: ActionName attribute
            var methods = controller.GetType().GetMethods().Where(m =>
                m.IsPublic && m.Name.Equals(values["action"], StringComparison.CurrentCultureIgnoreCase) &&
                typeof(ActionResult).IsAssignableFrom(m.ReturnType));
            foreach (var method in methods)
            {
                var parameters = method.GetParameters();
                actionParameters = new object[parameters.Length];
                int matches = 0;
                foreach (var parameter in parameters)
                {
                    var name = parameter.Name;

                    if (values.ContainsKey(name.ToUpper()))
                    {
                        try
                        {
                            actionParameters[matches] = Convert.ChangeType(values[name.ToUpper()], parameter.ParameterType);
                        }
                        catch
                        {
                            matches = -1;
                            break;
                        }
                        matches++;
                        continue;
                    }

                    if (request.QueryString.Any(q => q.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        try
                        {
                            actionParameters[matches] = Convert.ChangeType(request.QueryString.First(
                                q => q.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Value,
                                parameter.ParameterType);
                        }
                        catch
                        {
                            matches = -1;
                            break;
                        }
                        matches++;
                    }
                    if (request.Method == "POST" && request.Form != null)
                    {
                        if (request.Form.Any(q => q.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
                        {
                            try
                            {
                                var value = request.Form.First(
                                    q => q.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Value;
                                if (parameter.ParameterType == typeof(bool))
                                {
                                    if (value.ToUpper() == "ON" || value.ToUpper() == "OFF")
                                        value = value.ToUpper() == "ON" ? "true" : "false";
                                }
                                actionParameters[matches] = Convert.ChangeType(value, parameter.ParameterType);
                            }
                            catch
                            {
                                matches = -1;
                                break;
                            }
                            matches++;
                        }
                    }
                }
                if (matches == parameters.Length)
                    return method;
            }
            actionParameters = new object[0];
            return null;
        }