コード例 #1
0
        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            object identifierObj;

            if (!filterContext.RouteData.Values.TryGetValue(KeyConstants.Identifier, out identifierObj))
            {
                return;
            }

            object routeParamsObj;

            if (!filterContext.ParentActionViewContext.RouteData.DataTokens.TryGetValue("params", out routeParamsObj) || !(routeParamsObj is string[]))
            {
                return;
            }

            var routeParams = (string[])routeParamsObj;
            var identifier  = identifierObj.ToString();

            int identifierIndex = Array.FindIndex(routeParams, x => x == identifier);

            if (identifierIndex == -1)
            {
                return;
            }

            MatchCollection routeMatchCollection = ChildRouteHelper.GetChildRouteMatchCollection(Route);
            int             nextParamIndex       = identifierIndex + 1;

            foreach (Match routeMatch in routeMatchCollection)
            {
                var parameterDescriptor = filterContext.ActionDescriptor.GetParameters().FirstOrDefault(x => x.ParameterName == routeMatch.Value);
                if (parameterDescriptor == null || routeParams.Length <= nextParamIndex)
                {
                    return;
                }
                var routeParam = routeParams[nextParamIndex];
                try {
                    var parameterValue = TypeDescriptor.GetConverter(parameterDescriptor.ParameterType).ConvertFromInvariantString(routeParam);
                    filterContext.ActionParameters[routeMatch.Value] = parameterValue;
                }
                catch (Exception) {}
                nextParamIndex++;
            }
        }
コード例 #2
0
        private static RouteInformation GetRouteMatches(string identifier, Module[] modules)
        {
            ParameterInfo[]             parameters;
            Dictionary <string, string> defaults;
            Module module = modules.First(); //TODO: Really just take the first one?!
            Dictionary <string, Func <object, bool>[]> constraints;
            string          childRoute    = ChildRouteHelper.GetChildRoute(module.Action, module.Controller, out defaults, out parameters, out constraints);
            MatchCollection routeMachings = ChildRouteHelper.GetChildRouteMatchCollection(childRoute);

            string[]        childRouteMatches = (from Match routeMaching in routeMachings select routeMaching.Value).ToArray();
            ParameterInfo[] routeParameters   = parameters.Where(x => childRouteMatches.Contains(x.Name)).ToArray();

            return(new RouteInformation()
            {
                Identifier = identifier,
                RouteMatchings = childRouteMatches,
                Defaults = defaults,
                Modules = modules,
                RouteParameters = routeParameters,
                Constraints = constraints
            });
        }
コード例 #3
0
        private static string GenerateUrl(Uri uri, string action, string controller, string identifier, RouteValueDictionary routeValues)
        {
            Contract.Requires(uri != null);

            var absolutePath = uri.AbsolutePath;
            Dictionary <string, string> routeDefaults;
            string childRoute = ChildRouteHelper.GetChildRoute(action, controller, out routeDefaults);

            MatchCollection routeMachings = ChildRouteHelper.GetChildRouteMatchCollection(childRoute);

            string[] childRouteSegments = ChildRouteHelper.GetChildRouteSegments(routeValues, routeMachings, routeDefaults);

            var segmentIndex = GetIdentifierSegmentIndex(uri, identifier);

            if (segmentIndex == -1)
            {
                var childRoutePath = string.Join("/", childRouteSegments);
                absolutePath += $"/{identifier}/{childRoutePath}";
            }
            else
            {
                var newSegments     = uri.Segments;
                int routeValueCount = 0;
                for (int i = segmentIndex + 1; i < segmentIndex + routeMachings.Count + 1; i++)
                {
                    string lastSign = newSegments[i].LastOrDefault().ToString();
                    if (lastSign != "/")
                    {
                        lastSign = "";
                    }
                    newSegments[i] = childRouteSegments[routeValueCount] + lastSign;
                    routeValueCount++;
                }
                absolutePath = string.Join("", newSegments);
            }

            return(absolutePath + uri?.Query);
        }