Пример #1
0
        internal static ControllerAction GetControllerAction(string url)
        {
            RouteResult routeResult = WebAppConfig.Router.Resolve(url);

            if (routeResult == null)
            {
                return(null);
            }

            ControllerClass controllerClass = routeResult.CreateControllerClass();

            if (controllerClass != null)
            {
                ControllerAction controllerAction = new ControllerAction(controllerClass, routeResult.Action);

                foreach (KeyValuePair <string, string> param in routeResult.Parameters)
                {
                    controllerAction.Parameters.Add(param.Key, param.Value);
                }

                return(controllerAction);
            }

            return(null);
        }
Пример #2
0
        internal RouteResult Resolve(string url)
        {
            Match m = _regex.Match(url);

            if (m.Success)
            {
                RouteResult result = new RouteResult(Controller, Action, _parameters);

                foreach (string groupName in _regexGroups)
                {
                    string value = m.Groups[groupName].Value;
                    string searchString = "{" + groupName + "}";
                    bool found = false;

                    if (result.Controller.Contains(searchString))
                    {
                        result.Controller = result.Controller.Replace(searchString, value);
                        found = true;
                    }

                    if (result.Action.Contains(searchString))
                    {
                        result.Action = result.Action.Replace(searchString, value);
                        found = true;
                    }

                    if (_parameterNames != null)
                        foreach (string paramName in _parameterNames)
                        {
                            string paramValue = result.Parameters[paramName];

                            if (paramValue.Contains(searchString))
                            {
                                result.Parameters[paramName] = paramValue.Replace(searchString, value);
                                found = true;
                            }
                        }

                    if (!found)
                        result.Parameters[groupName] = value;

                    if (_validators != null)
                        foreach (IRouteValidator validator in _validators)
                            if (validator.ShouldValidate(groupName))
                            {
                                bool ok = validator.Validate(groupName, value);

                                RouteValidationAction actionToExecute = ok ? validator.PassAction : validator.FailAction;

                                switch (actionToExecute)
                                {
                                    case RouteValidationAction.Skip:
                                        result.ValidationResult = RouteValidationResult.Skip;
                                        break;
                                    case RouteValidationAction.Error:
                                        result.ValidationResult = RouteValidationResult.Fail;
                                        break;
                                    case RouteValidationAction.Accept:
                                        result.ValidationResult = RouteValidationResult.Success;
                                        break;
                                    case RouteValidationAction.None:
                                        continue;
                                }

                                return result;
                            }

                }

                return result;
            }

            return null;
        }