当前位置: 首页>>代码示例>>C#>>正文


C# HttpRouteValueDictionary.TryGetValue方法代码示例

本文整理汇总了C#中HttpRouteValueDictionary.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRouteValueDictionary.TryGetValue方法的具体用法?C# HttpRouteValueDictionary.TryGetValue怎么用?C# HttpRouteValueDictionary.TryGetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpRouteValueDictionary的用法示例。


在下文中一共展示了HttpRouteValueDictionary.TryGetValue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MatchContentPathSegment

        private static bool MatchContentPathSegment(PathContentSegment routeSegment, string requestPathSegment, HttpRouteValueDictionary defaultValues, HttpRouteValueDictionary matchedValues)
        {
            if (String.IsNullOrEmpty(requestPathSegment))
            {
                // If there's no data to parse, we must have exactly one parameter segment and no other segments - otherwise no match

                if (routeSegment.Subsegments.Count > 1)
                {
                    return false;
                }

                PathParameterSubsegment parameterSubsegment = routeSegment.Subsegments[0] as PathParameterSubsegment;
                if (parameterSubsegment == null)
                {
                    return false;
                }

                // We must have a default value since there's no value in the request URI
                object parameterValue;
                if (defaultValues.TryGetValue(parameterSubsegment.ParameterName, out parameterValue))
                {
                    // If there's a default value for this parameter, use that default value
                    matchedValues.Add(parameterSubsegment.ParameterName, parameterValue);
                    return true;
                }
                else
                {
                    // If there's no default value, this segment doesn't match
                    return false;
                }
            }

            // Find last literal segment and get its last index in the string

            int lastIndex = requestPathSegment.Length;
            int indexOfLastSegmentUsed = routeSegment.Subsegments.Count - 1;

            PathParameterSubsegment parameterNeedsValue = null; // Keeps track of a parameter segment that is pending a value
            PathLiteralSubsegment lastLiteral = null; // Keeps track of the left-most literal we've encountered

            while (indexOfLastSegmentUsed >= 0)
            {
                int newLastIndex = lastIndex;

                PathParameterSubsegment parameterSubsegment = routeSegment.Subsegments[indexOfLastSegmentUsed] as PathParameterSubsegment;
                if (parameterSubsegment != null)
                {
                    // Hold on to the parameter so that we can fill it in when we locate the next literal
                    parameterNeedsValue = parameterSubsegment;
                }
                else
                {
                    PathLiteralSubsegment literalSubsegment = routeSegment.Subsegments[indexOfLastSegmentUsed] as PathLiteralSubsegment;
                    if (literalSubsegment != null)
                    {
                        lastLiteral = literalSubsegment;

                        int startIndex = lastIndex - 1;
                        // If we have a pending parameter subsegment, we must leave at least one character for that
                        if (parameterNeedsValue != null)
                        {
                            startIndex--;
                        }

                        if (startIndex < 0)
                        {
                            return false;
                        }

                        int indexOfLiteral = requestPathSegment.LastIndexOf(literalSubsegment.Literal, startIndex, StringComparison.OrdinalIgnoreCase);
                        if (indexOfLiteral == -1)
                        {
                            // If we couldn't find this literal index, this segment cannot match
                            return false;
                        }

                        // If the first subsegment is a literal, it must match at the right-most extent of the request URI.
                        // Without this check if your route had "/Foo/" we'd match the request URI "/somethingFoo/".
                        // This check is related to the check we do at the very end of this function.
                        if (indexOfLastSegmentUsed == (routeSegment.Subsegments.Count - 1))
                        {
                            if ((indexOfLiteral + literalSubsegment.Literal.Length) != requestPathSegment.Length)
                            {
                                return false;
                            }
                        }

                        newLastIndex = indexOfLiteral;
                    }
                    else
                    {
                        Contract.Assert(false, "Invalid path segment type");
                    }
                }

                if ((parameterNeedsValue != null) && (((lastLiteral != null) && (parameterSubsegment == null)) || (indexOfLastSegmentUsed == 0)))
                {
                    // If we have a pending parameter that needs a value, grab that value

                    int parameterStartIndex;
//.........这里部分代码省略.........
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:101,代码来源:HttpParsedRoute.cs

示例2: MatchCatchAll

        private static void MatchCatchAll(PathContentSegment contentPathSegment, IEnumerable<string> remainingRequestSegments, HttpRouteValueDictionary defaultValues, HttpRouteValueDictionary matchedValues)
        {
            string remainingRequest = String.Join(String.Empty, remainingRequestSegments.ToArray());

            PathParameterSubsegment catchAllSegment = contentPathSegment.Subsegments[0] as PathParameterSubsegment;

            object catchAllValue;

            if (remainingRequest.Length > 0)
            {
                catchAllValue = remainingRequest;
            }
            else
            {
                defaultValues.TryGetValue(catchAllSegment.ParameterName, out catchAllValue);
            }

            matchedValues.Add(catchAllSegment.ParameterName, catchAllValue);
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:19,代码来源:HttpParsedRoute.cs

示例3: Bind

        public BoundRouteTemplate Bind(IDictionary<string, object> currentValues, IDictionary<string, object> values, HttpRouteValueDictionary defaultValues, HttpRouteValueDictionary constraints)
        {
            if (currentValues == null)
            {
                currentValues = new HttpRouteValueDictionary();
            }

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

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

            // The set of values we should be using when generating the URI in this route
            HttpRouteValueDictionary acceptedValues = new HttpRouteValueDictionary();

            // Keep track of which new values have been used
            HashSet<string> unusedNewValues = new HashSet<string>(values.Keys, StringComparer.OrdinalIgnoreCase);

            // Step 1: Get the list of values we're going to try to use to match and generate this URI

            // Find out which entries in the URI are valid for the URI we want to generate.
            // If the URI had ordered parameters a="1", b="2", c="3" and the new values
            // specified that b="9", then we need to invalidate everything after it. The new
            // values should then be a="1", b="9", c=<no value>.
            ForEachParameter(_pathSegments, delegate(PathParameterSubsegment parameterSubsegment)
            {
                // If it's a parameter subsegment, examine the current value to see if it matches the new value
                string parameterName = parameterSubsegment.ParameterName;

                object newParameterValue;
                bool hasNewParameterValue = values.TryGetValue(parameterName, out newParameterValue);
                if (hasNewParameterValue)
                {
                    unusedNewValues.Remove(parameterName);
                }

                object currentParameterValue;
                bool hasCurrentParameterValue = currentValues.TryGetValue(parameterName, out currentParameterValue);

                if (hasNewParameterValue && hasCurrentParameterValue)
                {
                    if (!RoutePartsEqual(currentParameterValue, newParameterValue))
                    {
                        // Stop copying current values when we find one that doesn't match
                        return false;
                    }
                }

                // If the parameter is a match, add it to the list of values we will use for URI generation
                if (hasNewParameterValue)
                {
                    if (IsRoutePartNonEmpty(newParameterValue))
                    {
                        acceptedValues.Add(parameterName, newParameterValue);
                    }
                }
                else
                {
                    if (hasCurrentParameterValue)
                    {
                        acceptedValues.Add(parameterName, currentParameterValue);
                    }
                }
                return true;
            });

            // Add all remaining new values to the list of values we will use for URI generation
            foreach (var newValue in values)
            {
                if (IsRoutePartNonEmpty(newValue.Value))
                {
                    if (!acceptedValues.ContainsKey(newValue.Key))
                    {
                        acceptedValues.Add(newValue.Key, newValue.Value);
                    }
                }
            }

            // Add all current values that aren't in the URI at all
            foreach (var currentValue in currentValues)
            {
                string parameterName = currentValue.Key;
                if (!acceptedValues.ContainsKey(parameterName))
                {
                    PathParameterSubsegment parameterSubsegment = GetParameterSubsegment(_pathSegments, parameterName);
                    if (parameterSubsegment == null)
                    {
                        acceptedValues.Add(parameterName, currentValue.Value);
                    }
                }
            }

            // Add all remaining default values from the route to the list of values we will use for URI generation
            ForEachParameter(_pathSegments, delegate(PathParameterSubsegment parameterSubsegment)
            {
//.........这里部分代码省略.........
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:101,代码来源:HttpParsedRoute.cs

示例4: IsParameterRequired

        private static bool IsParameterRequired(PathParameterSubsegment parameterSubsegment, HttpRouteValueDictionary defaultValues, out object defaultValue)
        {
            if (parameterSubsegment.IsCatchAll)
            {
                defaultValue = null;
                return false;
            }

            return !defaultValues.TryGetValue(parameterSubsegment.ParameterName, out defaultValue);
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:10,代码来源:HttpParsedRoute.cs

示例5: ProcessConstraint

        protected virtual bool ProcessConstraint(HttpRequestMessage request, object constraint, string parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
        {
            IHttpRouteConstraint customConstraint = constraint as IHttpRouteConstraint;
            if (customConstraint != null)
            {
                return customConstraint.Match(request, this, parameterName, values, routeDirection);
            }

            // If there was no custom constraint, then treat the constraint as a string which represents a Regex.
            string constraintsRule = constraint as string;
            if (constraintsRule == null)
            {
                throw Error.InvalidOperation(SRResources.Route_ValidationMustBeStringOrCustomConstraint, parameterName, RouteTemplate, typeof(IHttpRouteConstraint).Name);
            }

            object parameterValue;
            values.TryGetValue(parameterName, out parameterValue);
            string parameterValueString = Convert.ToString(parameterValue, CultureInfo.InvariantCulture);
            string constraintsRegEx = "^(" + constraintsRule + ")$";
            return Regex.IsMatch(parameterValueString, constraintsRegEx, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:21,代码来源:HttpRoute.cs


注:本文中的HttpRouteValueDictionary.TryGetValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。