Exemple #1
0
        /// <summary>
        /// Gets the value of the route parameter
        /// with the specified name
        /// </summary>
        /// <param name="key">The name of the
        /// parameter to find</param>
        public object GetValue(string key)
        {
            if (_values.ContainsKey(key))
            {
                return(_values[key]);
            }

            return(null);
        }
        public BoundUrl Bind(ValueDictionary currentValues, 
            ValueDictionary values, ValueDictionary variables, 
			ValueDictionary defaults)
        {
            currentValues = currentValues ?? new ValueDictionary();
            values = values ?? new ValueDictionary();
			variables = variables ?? new ValueDictionary();
			defaults = defaults ?? new ValueDictionary();

            ValueDictionary acceptedValues = new ValueDictionary();
            HashSet<string> unusedValues = new HashSet<string>(
                values.Keys, StringComparer.OrdinalIgnoreCase);

			AssignVariableValues(variables);

            ForEachParameter(_segments, segment => {
                object value;
                object currentValue;

                string parameterName = segment.ParameterName;
                bool hasValue = values.TryGetValue(parameterName, out value);
                
                if (hasValue)
                    unusedValues.Remove(parameterName);
                
                bool hasCurrentValue = currentValues.TryGetValue(parameterName, out currentValue);
                
                if (hasValue && hasCurrentValue && !RoutePartsEqual(currentValue, value))
                    return false;

                if (hasValue)
                {
                    if (IsRoutePartNonEmpty(value))
                        acceptedValues.Add(parameterName, value);
                }
                else if (hasCurrentValue)
                    acceptedValues.Add(parameterName, currentValue);
                
                return true;
            });

            foreach (KeyValuePair<string, object> kvp in values)
            {
                if (IsRoutePartNonEmpty(kvp.Value) && !acceptedValues.ContainsKey(kvp.Key))
                    acceptedValues.Add(kvp.Key, kvp.Value);
            }

            foreach (KeyValuePair<string, object> kvp in currentValues)
            {
                if (!acceptedValues.ContainsKey(kvp.Key) && 
                    GetParameterSubsegment(_segments, kvp.Key) == null)
                    acceptedValues.Add(kvp.Key, kvp.Value);
            }

            ForEachParameter(_segments, segment => {
                object value;
                
                if (!acceptedValues.ContainsKey(segment.ParameterName) && 
                    !IsParameterRequired(segment, defaults, out value))
                    acceptedValues.Add(segment.ParameterName, value);
                
                return true;
            });

            if (!ForEachParameter(_segments, segment => {
                object value;
                if (IsParameterRequired(segment, defaults, out value) && 
                    !acceptedValues.ContainsKey(segment.ParameterName))
                    return false;
                
                return true;
            })) 
            {
                return null;
            }

            ValueDictionary others = new ValueDictionary(
                (IDictionary<string, object>)defaults);
            ForEachParameter(_segments, segment => {
                others.Remove(segment.ParameterName);
                return true;
            });

            foreach (KeyValuePair<string, object> kvp in others)
            {
                object value;
                if (values.TryGetValue(kvp.Key, out value))
                {
                    unusedValues.Remove(kvp.Key);
                    if (!RoutePartsEqual(value, kvp.Value))
                        return null;
                }
            }

            return BuildUrl(defaults, acceptedValues, unusedValues);
        }
        public ValueDictionary Match(string virtualPath, 
			ValueDictionary variables, ValueDictionary defaults)
        {
            List<string> parts = new List<string>(RouteParser.SplitUrl(virtualPath));
            
			defaults = defaults ?? new ValueDictionary();
			variables = variables ?? new ValueDictionary();
            
            ValueDictionary values = new ValueDictionary();

            bool hasAdditionalParameters = false;
            bool isCatchAll = false;

			AssignVariableValues(variables);

            for (int i = 0; i < _segments.Count; i++)
            {
                SeparatorSegment separator = (_segments[i] as SeparatorSegment);
                ContentSegment content = (_segments[i] as ContentSegment);

                if (parts.Count <= i)
                    hasAdditionalParameters = true;
                
                string part = (hasAdditionalParameters) ? null : parts[i];
                if (separator != null)
                {
                    if (!hasAdditionalParameters && 
                        !RouteParser.IsSeparator(part))
                        return null;
                }
                
                if (content != null)
                {
                    if (content.IsCatchAll)
                    {
                        MatchCatchAll(content, parts.Skip(i), defaults, values);
                        isCatchAll = true;
                    }
                    else if (!MatchContent(content, part, defaults, values))
                        return null;
                }
            }

            if (!isCatchAll && _segments.Count < parts.Count)
            {
                for (int j = _segments.Count; j < parts.Count; j++)
                {
                    if (!RouteParser.IsSeparator(parts[j]))
                        return null;
                }
            }

            if (defaults != null)
            {
                foreach (KeyValuePair<string, object> kvp in defaults)
                {
                    if (!values.ContainsKey(kvp.Key))
                        values.Add(kvp.Key, kvp.Value);
                }
            }

            return values;
        }
 /// <inheritdoc />
 public bool ContainsKey(string key) => ValueDictionary.ContainsKey(key);
        public BoundUrl Bind(ValueDictionary currentValues,
                             ValueDictionary values, ValueDictionary variables,
                             ValueDictionary defaults)
        {
            currentValues = currentValues ?? new ValueDictionary();
            values        = values ?? new ValueDictionary();
            variables     = variables ?? new ValueDictionary();
            defaults      = defaults ?? new ValueDictionary();

            ValueDictionary  acceptedValues = new ValueDictionary();
            HashSet <string> unusedValues   = new HashSet <string>(
                values.Keys, StringComparer.OrdinalIgnoreCase);

            AssignVariableValues(variables);

            ForEachParameter(_segments, segment => {
                object value;
                object currentValue;

                string parameterName = segment.ParameterName;
                bool hasValue        = values.TryGetValue(parameterName, out value);

                if (hasValue)
                {
                    unusedValues.Remove(parameterName);
                }

                bool hasCurrentValue = currentValues.TryGetValue(parameterName, out currentValue);

                if (hasValue && hasCurrentValue && !RoutePartsEqual(currentValue, value))
                {
                    return(false);
                }

                if (hasValue)
                {
                    if (IsRoutePartNonEmpty(value))
                    {
                        acceptedValues.Add(parameterName, value);
                    }
                }
                else if (hasCurrentValue)
                {
                    acceptedValues.Add(parameterName, currentValue);
                }

                return(true);
            });

            foreach (KeyValuePair <string, object> kvp in values)
            {
                if (IsRoutePartNonEmpty(kvp.Value) && !acceptedValues.ContainsKey(kvp.Key))
                {
                    acceptedValues.Add(kvp.Key, kvp.Value);
                }
            }

            foreach (KeyValuePair <string, object> kvp in currentValues)
            {
                if (!acceptedValues.ContainsKey(kvp.Key) &&
                    GetParameterSubsegment(_segments, kvp.Key) == null)
                {
                    acceptedValues.Add(kvp.Key, kvp.Value);
                }
            }

            ForEachParameter(_segments, segment => {
                object value;

                if (!acceptedValues.ContainsKey(segment.ParameterName) &&
                    !IsParameterRequired(segment, defaults, out value))
                {
                    acceptedValues.Add(segment.ParameterName, value);
                }

                return(true);
            });

            if (!ForEachParameter(_segments, segment => {
                object value;
                if (IsParameterRequired(segment, defaults, out value) &&
                    !acceptedValues.ContainsKey(segment.ParameterName))
                {
                    return(false);
                }

                return(true);
            }))
            {
                return(null);
            }

            ValueDictionary others = new ValueDictionary(
                (IDictionary <string, object>)defaults);

            ForEachParameter(_segments, segment => {
                others.Remove(segment.ParameterName);
                return(true);
            });

            foreach (KeyValuePair <string, object> kvp in others)
            {
                object value;
                if (values.TryGetValue(kvp.Key, out value))
                {
                    unusedValues.Remove(kvp.Key);
                    if (!RoutePartsEqual(value, kvp.Value))
                    {
                        return(null);
                    }
                }
            }

            return(BuildUrl(defaults, acceptedValues, unusedValues));
        }
        public ValueDictionary Match(string virtualPath,
                                     ValueDictionary variables, ValueDictionary defaults)
        {
            List <string> parts = new List <string>(RouteParser.SplitUrl(virtualPath));

            defaults  = defaults ?? new ValueDictionary();
            variables = variables ?? new ValueDictionary();

            ValueDictionary values = new ValueDictionary();

            bool hasAdditionalParameters = false;
            bool isCatchAll = false;

            AssignVariableValues(variables);

            for (int i = 0; i < _segments.Count; i++)
            {
                SeparatorSegment separator = (_segments[i] as SeparatorSegment);
                ContentSegment   content   = (_segments[i] as ContentSegment);

                if (parts.Count <= i)
                {
                    hasAdditionalParameters = true;
                }

                string part = (hasAdditionalParameters) ? null : parts[i];
                if (separator != null)
                {
                    if (!hasAdditionalParameters &&
                        !RouteParser.IsSeparator(part))
                    {
                        return(null);
                    }
                }

                if (content != null)
                {
                    if (content.IsCatchAll)
                    {
                        MatchCatchAll(content, parts.Skip(i), defaults, values);
                        isCatchAll = true;
                    }
                    else if (!MatchContent(content, part, defaults, values))
                    {
                        return(null);
                    }
                }
            }

            if (!isCatchAll && _segments.Count < parts.Count)
            {
                for (int j = _segments.Count; j < parts.Count; j++)
                {
                    if (!RouteParser.IsSeparator(parts[j]))
                    {
                        return(null);
                    }
                }
            }

            if (defaults != null)
            {
                foreach (KeyValuePair <string, object> kvp in defaults)
                {
                    if (!values.ContainsKey(kvp.Key))
                    {
                        values.Add(kvp.Key, kvp.Value);
                    }
                }
            }

            return(values);
        }
Exemple #7
0
 public bool ContainsKey(string key)
 {
     return(_values.ContainsKey(key));
 }