Encapsulates application logic exposed as a set of actions. The controller itself should be stateless. Each action is invoked using a different controller instance.
Inheritance: IController
        public static ActionDescriptor GetAction(Controller controller, string actionName)
        {
            ControllerDescriptor controllerDescriptor = GetDescriptor(controller.GetType());
            ActionDescriptor actionDescriptor;

            controllerDescriptor._actions.TryGetValue(actionName, out actionDescriptor);
            return actionDescriptor;
        }
Esempio n. 2
0
        public object Invoke(Controller controller, ActionInvocation action)
        {
            ParameterInfo[] parameters = _actionMethod.GetParameters();
            object[] parameterValues = null;

            if (parameters.Length != 0) {
                List<object> parameterList = new List<object>(parameters.Length);

                int paramIndex = 0;
                for (; paramIndex < action.Parameters.Count && paramIndex < parameters.Length; paramIndex++) {
                    parameterList.Add(action.Parameters[paramIndex]);
                }

                for (; paramIndex < parameters.Length; paramIndex++) {
                    string paramName = parameters[paramIndex].Name;
                    string paramValue = null;

                    action.NamedParameters.TryGetValue(paramName, out paramValue);
                    parameterList.Add(paramValue);
                }

                for (int i = 0; i < parameterList.Count; i++) {
                    Type parameterType = parameters[i].ParameterType;

                    object paramValue = parameterList[i];
                    if ((paramValue == null) || (parameterType.IsAssignableFrom(paramValue.GetType()) == false)) {
                        paramValue = Convert.ChangeType(paramValue, parameterType, CultureInfo.CurrentCulture);
                        parameterList[i] = paramValue;
                    }
                }

                parameterValues = parameterList.ToArray();
            }

            return _actionMethod.Invoke(controller, parameterValues);
        }
Esempio n. 3
0
 public ActionAsyncResult(Controller controller, AsyncCallback callback, object asyncState)
 {
     _controller = controller;
     _callback = callback;
     _asyncState = asyncState;
 }
Esempio n. 4
0
        /// <internalonly />
        protected override IAsyncResult BeginLoadUri(Uri uri, Page uriContext, AsyncCallback callback, object asyncState)
        {
            if (_currentController != null) {
                throw new InvalidOperationException();
            }

            UriData uriData = new UriData(uri);

            IList<string> path = uriData.GetPath();
            int minimumCount = _controllerType != null ? 1 : 2;

            if ((path != null) && (path.Count >= minimumCount)) {
                Controller controller = null;

                Type controllerType = _controllerType;
                if (controllerType == null) {
                    Type appType = Application.Current.GetType();

                    _currentControllerName = path[0];
                    path.RemoveAt(0);

                    string controllerTypeName =
                        appType.Namespace + ".Controllers." + _currentControllerName + "Controller";

                    controllerType = appType.Assembly.GetType(controllerTypeName, /* throwOnError */ false);
                    if (typeof(Controller).IsAssignableFrom(controllerType) == false) {
                        controllerType = null;
                    }
                }
                if (controllerType != null) {
                    controller = CreateController(controllerType);
                }

                if (controller != null) {
                    _currentController = controller;

                    _currentActionName = path[0];
                    path.RemoveAt(0);

                    ActionInvocation action = new ActionInvocation(_currentActionName);
                    foreach (string parameterItem in path) {
                        action.Parameters.Add(parameterItem);
                    }

                    IDictionary<string, string> queryString = uriData.GetQueryString();
                    if (queryString != null) {
                        foreach (KeyValuePair<string, string> parameterItem in queryString) {
                            action.NamedParameters[parameterItem.Key] = parameterItem.Value;
                        }
                    }

                    return ((IController)controller).BeginExecute(action, callback, asyncState);
                }
            }

            return base.BeginLoadUri(uri, uriContext, callback, asyncState);
        }
Esempio n. 5
0
        /// <internalonly />
        protected override object EndLoadUri(IAsyncResult asyncResult, out Uri redirectUri)
        {
            redirectUri = null;

            if (_currentController != null) {
                ActionResult actionResult = null;
                try {
                    actionResult = ((IController)_currentController).EndExecute(asyncResult);

                    RedirectActionResult redirectResult = actionResult as RedirectActionResult;
                    if (redirectResult != null) {
                        StringBuilder uriBuilder = new StringBuilder("/");

                        if (_controllerType == null) {
                            string controllerName = redirectResult.ControllerType.Name;
                            if (controllerName.EndsWith("Controller", StringComparison.Ordinal)) {
                                controllerName = controllerName.Substring(0, controllerName.Length - 10);
                            }
                            uriBuilder.Append(controllerName);
                            uriBuilder.Append("/");
                        }

                        uriBuilder.Append(redirectResult.ActionName);

                        if (redirectResult.Parameters != null) {
                            foreach (string parameter in redirectResult.Parameters) {
                                uriBuilder.Append("/");
                                uriBuilder.Append(parameter);
                            }
                        }

                        if (redirectResult.HasNamedParameters) {
                            uriBuilder.Append("?");
                            foreach (KeyValuePair<string, string> parameter in redirectResult.NamedParmeters) {
                                uriBuilder.AppendFormat("{0}={1}&", parameter.Key, parameter.Value);
                            }
                        }

                        redirectUri = new Uri(uriBuilder.ToString(), UriKind.Relative);
                    }
                }
                finally {
                    _currentController = null;
                }
                return actionResult;
            }

            return base.EndLoadUri(asyncResult, out redirectUri);
        }