private TemplatePart ParseParameter(string routeParameter)
    {
        var _constraintResolver = GetConstraintResolver();
        var templatePart        = InlineRouteParameterParser.ParseRouteParameter(routeParameter);

        return(templatePart);
    }
Пример #2
0
        private TemplatePart ParseParameter(string routeParameter)
        {
            var _constraintResolver = GetConstraintResolver();

#pragma warning disable CS0618 // Type or member is obsolete
            var templatePart = InlineRouteParameterParser.ParseRouteParameter(routeParameter);
#pragma warning restore CS0618 // Type or member is obsolete
            return(templatePart);
        }
Пример #3
0
        private static bool ParseParameter(TemplateParserContext context, TemplateSegment segment)
        {
            context.Mark();

            while (true)
            {
                if (context.Current == Separator)
                {
                    // This is a dangling open-brace, which is not allowed
                    context.Error = Resources.TemplateRoute_MismatchedParameter;
                    return(false);
                }
                else if (context.Current == OpenBrace)
                {
                    // If we see a '{' while parsing a parameter name it's invalid. We'll just accept it for now
                    // and let the validation code for the name find it.
                }
                else if (context.Current == CloseBrace)
                {
                    if (!context.Next())
                    {
                        // This is the end of the string - and we have a valid parameter
                        context.Back();
                        break;
                    }

                    if (context.Current == CloseBrace)
                    {
                        // This is an 'escaped' brace in a parameter name, which is not allowed.
                        // We'll just accept it for now and let the validation code for the name find it.
                    }
                    else
                    {
                        // This is the end of the parameter
                        context.Back();
                        break;
                    }
                }

                if (!context.Next())
                {
                    // This is a dangling open-brace, which is not allowed
                    context.Error = Resources.TemplateRoute_MismatchedParameter;
                    return(false);
                }
            }

            var rawParameter = context.Capture();

            // At this point, we need to parse the raw name for inline constraint,
            // default values and optional parameters.
            var templatePart = InlineRouteParameterParser.ParseRouteParameter(rawParameter);

            if (templatePart.IsCatchAll && templatePart.IsOptional)
            {
                context.Error = Resources.TemplateRoute_CatchAllCannotBeOptional;
                return(false);
            }

            if (templatePart.IsOptional && templatePart.DefaultValue != null)
            {
                // Cannot be optional and have a default value.
                // The only way to declare an optional parameter is to have a ? at the end,
                // hence we cannot have both default value and optional parameter within the template.
                // A workaround is to add it as a separate entry in the defaults argument.
                context.Error = Resources.TemplateRoute_OptionalCannotHaveDefaultValue;
                return(false);
            }

            var parameterName = templatePart.Name;

            if (IsValidParameterName(context, parameterName))
            {
                segment.Parts.Add(templatePart);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private static bool ParseParameter(TemplateParserContext context, TemplateSegment segment)
        {
            context.Mark();

            while (true)
            {
                if (context.Current == OpenBrace)
                {
                    // This is an open brace inside of a parameter, it has to be escaped
                    if (context.Next())
                    {
                        if (context.Current != OpenBrace)
                        {
                            // If we see something like "{p1:regex(^\d{3", we will come here.
                            context.Error = Resources.TemplateRoute_UnescapedBrace;
                            return(false);
                        }
                    }
                    else
                    {
                        // This is a dangling open-brace, which is not allowed
                        // Example: "{p1:regex(^\d{"
                        context.Error = Resources.TemplateRoute_MismatchedParameter;
                        return(false);
                    }
                }
                else if (context.Current == CloseBrace)
                {
                    // When we encounter Closed brace here, it either means end of the parameter or it is a closed
                    // brace in the parameter, in that case it needs to be escaped.
                    // Example: {p1:regex(([}}])\w+}. First pair is escaped one and last marks end of the parameter
                    if (!context.Next())
                    {
                        // This is the end of the string -and we have a valid parameter
                        context.Back();
                        break;
                    }

                    if (context.Current == CloseBrace)
                    {
                        // This is an 'escaped' brace in a parameter name
                    }
                    else
                    {
                        // This is the end of the parameter
                        context.Back();
                        break;
                    }
                }

                if (!context.Next())
                {
                    // This is a dangling open-brace, which is not allowed
                    context.Error = Resources.TemplateRoute_MismatchedParameter;
                    return(false);
                }
            }

            var rawParameter = context.Capture();
            var decoded      = rawParameter.Replace("}}", "}").Replace("{{", "{");

            // At this point, we need to parse the raw name for inline constraint,
            // default values and optional parameters.
            var templatePart = InlineRouteParameterParser.ParseRouteParameter(decoded);

            if (templatePart.IsCatchAll && templatePart.IsOptional)
            {
                context.Error = Resources.TemplateRoute_CatchAllCannotBeOptional;
                return(false);
            }

            if (templatePart.IsOptional && templatePart.DefaultValue != null)
            {
                // Cannot be optional and have a default value.
                // The only way to declare an optional parameter is to have a ? at the end,
                // hence we cannot have both default value and optional parameter within the template.
                // A workaround is to add it as a separate entry in the defaults argument.
                context.Error = Resources.TemplateRoute_OptionalCannotHaveDefaultValue;
                return(false);
            }

            var parameterName = templatePart.Name;

            if (IsValidParameterName(context, parameterName))
            {
                segment.Parts.Add(templatePart);
                return(true);
            }
            else
            {
                return(false);
            }
        }