Exemple #1
0
        public List <UrlSegment> CreateSegments()
        {
            var segments = new List <UrlSegment>();

            foreach (var segment in UrlPattern.Split('/'))
            {
                if (segment.StartsWith("{") && segment.EndsWith("}"))
                {
                    segments.Add(new UrlSegment {
                        Segment = segment.Replace("{", "").Replace("}", ""), IsVariable = true
                    });
                }
                else
                {
                    segments.Add(new UrlSegment {
                        Segment = segment
                    });
                }
            }
            return(segments);
        }
        private void ParseUrlPattern()
        {
            if (String.IsNullOrEmpty(UrlPattern) || String.IsNullOrWhiteSpace(UrlPattern))
            {
                SetInvalid("Url pattern is null, empty, or nothing but whitespace");

                return;
            }

            if (UrlPattern[0] != '/')
            {
                SetInvalid("Url pattern must open with '/'");

                return;
            }

            string[] matches = UrlPattern.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (matches == null || matches.Length == 0)
            {
                SillySegment rootSegment = new SillyHardCodedSegment();

                Add(rootSegment);

                return;
            }

            bool controllerSet = false, methodSet = false;

            foreach (string match in matches)
            {
                if (String.Compare("/", match, true) == 0)
                {
                    continue;
                }

                SillySegment segment = ProduceFromSegment(match);

                if (segment == null)
                {
                    SetInvalid("Cannot find a suitable match for Url segment '" + match + "'");

                    return;
                }

                if (segment.Type == SegmentTypes.Controller)
                {
                    if (controllerSet)
                    {
                        SetInvalid("Cannot define multiple :controller segments in a Url pattern");

                        return;
                    }

                    controllerSet = true;

                    if (methodSet)
                    {
                        SetInvalid("Cannot define :method before :controller in a Url pattern");

                        return;
                    }

                    if (VarCount > 0)
                    {
                        SetInvalid("Cannot define {vars} before :controller in a Url pattern");

                        return;
                    }
                }

                if (segment.Type == SegmentTypes.Method)
                {
                    if (methodSet)
                    {
                        SetInvalid("Cannot define multiple :method segments in a Url pattern");

                        return;
                    }

                    methodSet = true;

                    if (VarCount > 0)
                    {
                        SetInvalid("Cannot define {vars} before :method in a Url pattern");

                        return;
                    }
                }

                Add(segment);
            }
        }