コード例 #1
0
        internal virtual DirectRouteBuilder CreateBuilder(string template,
                                                          IInlineConstraintResolver constraintResolver)
        {
            DirectRouteBuilder builder = new DirectRouteBuilder(_actions);

            string prefixedTemplate = BuildRouteTemplate(_prefix, template);

            Contract.Assert(prefixedTemplate != null);

            if (prefixedTemplate.StartsWith("/", StringComparison.Ordinal))
            {
                throw Error.InvalidOperation(SRResources.AttributeRoutes_InvalidTemplate, template, _actionName);
            }

            if (constraintResolver != null)
            {
                builder.Defaults    = new HttpRouteValueDictionary();
                builder.Constraints = new HttpRouteValueDictionary();
                builder.Template    = InlineRouteTemplateParser.ParseRouteTemplate(prefixedTemplate, builder.Defaults,
                                                                                   builder.Constraints, constraintResolver);
                builder.ParsedRoute = HttpRouteParser.Parse(builder.Template);
                builder.Precedence  = RoutePrecedence.Compute(builder.ParsedRoute, builder.Constraints);
            }
            else
            {
                builder.Template = prefixedTemplate;
            }

            return(builder);
        }
コード例 #2
0
        public HttpRoute(string routeTemplate, HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints, HttpRouteValueDictionary dataTokens)
        {
            _routeTemplate = String.IsNullOrWhiteSpace(routeTemplate) ? String.Empty : routeTemplate;
            _defaults      = defaults ?? new HttpRouteValueDictionary();
            _constraints   = constraints ?? new HttpRouteValueDictionary();
            _dataTokens    = dataTokens ?? new HttpRouteValueDictionary();

            // The parser will throw for invalid routes.
            _parsedRoute = HttpRouteParser.Parse(_routeTemplate);
        }
コード例 #3
0
        public HttpRoute(string routeTemplate, HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints, HttpRouteValueDictionary dataTokens, HttpMessageHandler handler)
        {
            _routeTemplate = routeTemplate == null ? String.Empty : routeTemplate;
            _defaults      = defaults ?? new HttpRouteValueDictionary();
            _constraints   = constraints ?? new HttpRouteValueDictionary();
            _dataTokens    = dataTokens ?? new HttpRouteValueDictionary();
            Handler        = handler;

            // The parser will throw for invalid routes.
            ParsedRoute = HttpRouteParser.Parse(RouteTemplate);
        }
コード例 #4
0
        private static decimal GetPrecedence(string attributeRouteTemplate)
        {
            DefaultInlineConstraintResolver resolver    = new DefaultInlineConstraintResolver();
            HttpRouteValueDictionary        defaults    = new HttpRouteValueDictionary();
            HttpRouteValueDictionary        constraints = new HttpRouteValueDictionary();
            string standardRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(attributeRouteTemplate,
                                                                                        defaults, constraints, new DefaultInlineConstraintResolver());
            HttpParsedRoute parsedRoute = HttpRouteParser.Parse(standardRouteTemplate);

            return(parsedRoute.GetPrecedence(constraints));
        }
コード例 #5
0
        public HttpRouteValueDictionary Match(string virtualPath, HttpRouteValueDictionary defaultValues)
        {
            IList <string> requestPathSegments = HttpRouteParser.SplitUriToPathSegmentStrings(virtualPath);

            if (defaultValues == null)
            {
                defaultValues = new HttpRouteValueDictionary();
            }

            HttpRouteValueDictionary matchedValues = new HttpRouteValueDictionary();

            // This flag gets set once all the data in the URI has been parsed through, but
            // the route we're trying to match against still has more parts. At this point
            // we'll only continue matching separator characters and parameters that have
            // default values.
            bool ranOutOfStuffToParse = false;

            // This value gets set once we start processing a catchall parameter (if there is one
            // at all). Once we set this value we consume all remaining parts of the URI into its
            // parameter value.
            bool usedCatchAllParameter = false;

            for (int i = 0; i < _pathSegments.Count; i++)
            {
                PathSegment pathSegment = _pathSegments[i];

                if (requestPathSegments.Count <= i)
                {
                    ranOutOfStuffToParse = true;
                }

                string requestPathSegment = ranOutOfStuffToParse ? null : requestPathSegments[i];

                if (pathSegment is PathSeparatorSegment)
                {
                    if (ranOutOfStuffToParse)
                    {
                        // If we're trying to match a separator in the route but there's no more content, that's OK
                    }
                    else
                    {
                        if (!String.Equals(requestPathSegment, "/", StringComparison.Ordinal))
                        {
                            return(null);
                        }
                    }
                }
                else
                {
                    PathContentSegment contentPathSegment = pathSegment as PathContentSegment;
                    if (contentPathSegment != null)
                    {
                        if (contentPathSegment.IsCatchAll)
                        {
                            Contract.Assert(i == (_pathSegments.Count - 1), "If we're processing a catch-all, we should be on the last route segment.");
                            MatchCatchAll(contentPathSegment, requestPathSegments.Skip(i), defaultValues, matchedValues);
                            usedCatchAllParameter = true;
                        }
                        else
                        {
                            if (!MatchContentPathSegment(contentPathSegment, requestPathSegment, defaultValues, matchedValues))
                            {
                                return(null);
                            }
                        }
                    }
                    else
                    {
                        Contract.Assert(false, "Invalid path segment type");
                    }
                }
            }

            if (!usedCatchAllParameter)
            {
                if (_pathSegments.Count < requestPathSegments.Count)
                {
                    // If we've already gone through all the parts defined in the route but the URI
                    // still contains more content, check that the remaining content is all separators.
                    for (int i = _pathSegments.Count; i < requestPathSegments.Count; i++)
                    {
                        if (!HttpRouteParser.IsSeparator(requestPathSegments[i]))
                        {
                            return(null);
                        }
                    }
                }
            }

            // Copy all remaining default values to the route data
            if (defaultValues != null)
            {
                foreach (var defaultValue in defaultValues)
                {
                    if (!matchedValues.ContainsKey(defaultValue.Key))
                    {
                        matchedValues.Add(defaultValue.Key, defaultValue.Value);
                    }
                }
            }

            return(matchedValues);
        }