@@ -18,93 +18,88 @@ internal static RouteTemplate ParseTemplate(string template)
18
18
var originalTemplate = template ;
19
19
20
20
template = template . Trim ( '/' ) ;
21
-
22
- if ( template == string . Empty )
23
- {
24
- throw new InvalidOperationException (
25
- $ "Empty routes are not allowed. Use a catchall route instead.") ;
26
- }
27
-
28
- var segments = template . Split ( '/' ) ;
29
- var templateSegments = new List < TemplateSegment > ( segments . Length ) ;
30
-
31
- for ( int i = 0 ; i < segments . Length ; i ++ )
21
+ var templateSegments = new List < TemplateSegment > ( ) ;
22
+ if ( template != string . Empty )
32
23
{
33
- var segment = segments [ i ] ;
34
-
35
- if ( string . IsNullOrEmpty ( segment ) )
24
+ var segments = template . Split ( '/' ) ;
25
+ templateSegments = new List < TemplateSegment > ( segments . Length ) ;
26
+ for ( int i = 0 ; i < segments . Length ; i ++ )
36
27
{
37
- throw new InvalidOperationException (
38
- $ "Invalid template '{ template } '. Empty segments are not allowed.") ;
39
- }
28
+ var segment = segments [ i ] ;
40
29
41
- if ( segment [ 0 ] != '{' )
42
- {
43
- if ( segment [ segment . Length - 1 ] == '}' )
30
+ if ( string . IsNullOrEmpty ( segment ) )
44
31
{
45
32
throw new InvalidOperationException (
46
- $ "Invalid template '{ template } '. Missing '{{' in parameter segment ' { segment } ' .") ;
33
+ $ "Invalid template '{ template } '. Empty segments are not allowed .") ;
47
34
}
48
35
49
- templateSegments . Add ( new TemplateSegment ( originalTemplate , segment , isParameter : false ) ) ;
50
- }
51
- else
52
- {
53
- if ( segment [ segment . Length - 1 ] != '}' )
36
+ if ( segment [ 0 ] != '{' )
54
37
{
55
- throw new InvalidOperationException (
56
- $ "Invalid template '{ template } '. Missing '}}' in parameter segment '{ segment } '.") ;
57
- }
38
+ if ( segment [ segment . Length - 1 ] == '}' )
39
+ {
40
+ throw new InvalidOperationException (
41
+ $ "Invalid template '{ template } '. Missing '{{' in parameter segment '{ segment } '.") ;
42
+ }
58
43
59
- if ( segment . Length < 3 )
60
- {
61
- throw new InvalidOperationException (
62
- $ "Invalid template '{ template } '. Empty parameter name in segment '{ segment } ' is not allowed.") ;
44
+ templateSegments . Add ( new TemplateSegment ( originalTemplate , segment , isParameter : false ) ) ;
63
45
}
64
-
65
- var invalidCharacter = segment . IndexOfAny ( InvalidParameterNameCharacters , 1 , segment . Length - 2 ) ;
66
-
67
- if ( invalidCharacter != - 1 )
46
+ else
68
47
{
69
- throw new InvalidOperationException (
70
- $ "Invalid template '{ template } '. The character '{ segment [ invalidCharacter ] } ' in parameter segment '{ segment } ' is not allowed.") ;
48
+ if ( segment [ segment . Length - 1 ] != '}' )
49
+ {
50
+ throw new InvalidOperationException (
51
+ $ "Invalid template '{ template } '. Missing '}}' in parameter segment '{ segment } '.") ;
52
+ }
53
+
54
+ if ( segment . Length < 3 )
55
+ {
56
+ throw new InvalidOperationException (
57
+ $ "Invalid template '{ template } '. Empty parameter name in segment '{ segment } ' is not allowed.") ;
58
+ }
59
+
60
+ var invalidCharacter = segment . IndexOfAny ( InvalidParameterNameCharacters , 1 , segment . Length - 2 ) ;
61
+
62
+ if ( invalidCharacter != - 1 )
63
+ {
64
+ throw new InvalidOperationException (
65
+ $ "Invalid template '{ template } '. The character '{ segment [ invalidCharacter ] } ' in parameter segment '{ segment } ' is not allowed.") ;
66
+ }
67
+
68
+ templateSegments . Add ( new TemplateSegment ( originalTemplate , segment . Substring ( 1 , segment . Length - 2 ) , isParameter : true ) ) ;
71
69
}
72
-
73
- templateSegments . Add ( new TemplateSegment ( originalTemplate , segment . Substring ( 1 , segment . Length - 2 ) , isParameter : true ) ) ;
74
70
}
75
- }
76
71
77
- for ( int i = 0 ; i < templateSegments . Count ; i ++ )
78
- {
79
- var currentSegment = templateSegments [ i ] ;
80
-
81
- if ( currentSegment . IsCatchAll && i != templateSegments . Count - 1 )
72
+ for ( int i = 0 ; i < templateSegments . Count ; i ++ )
82
73
{
83
- throw new InvalidOperationException ( $ "Invalid template '{ template } '. A catch-all parameter can only appear as the last segment of the route template.") ;
84
- }
74
+ var currentSegment = templateSegments [ i ] ;
85
75
86
- if ( ! currentSegment . IsParameter )
87
- {
88
- continue ;
89
- }
90
-
91
- for ( int j = i + 1 ; j < templateSegments . Count ; j ++ )
92
- {
93
- var nextSegment = templateSegments [ j ] ;
76
+ if ( currentSegment . IsCatchAll && i != templateSegments . Count - 1 )
77
+ {
78
+ throw new InvalidOperationException ( $ "Invalid template '{ template } '. A catch-all parameter can only appear as the last segment of the route template.") ;
79
+ }
94
80
95
- if ( currentSegment . IsOptional && ! nextSegment . IsOptional )
81
+ if ( ! currentSegment . IsParameter )
96
82
{
97
- throw new InvalidOperationException ( $ "Invalid template ' { template } '. Non-optional parameters or literal routes cannot appear after optional parameters." ) ;
83
+ continue ;
98
84
}
99
85
100
- if ( string . Equals ( currentSegment . Value , nextSegment . Value , StringComparison . OrdinalIgnoreCase ) )
86
+ for ( int j = i + 1 ; j < templateSegments . Count ; j ++ )
101
87
{
102
- throw new InvalidOperationException (
103
- $ "Invalid template '{ template } '. The parameter '{ currentSegment } ' appears multiple times.") ;
88
+ var nextSegment = templateSegments [ j ] ;
89
+
90
+ if ( currentSegment . IsOptional && ! nextSegment . IsOptional )
91
+ {
92
+ throw new InvalidOperationException ( $ "Invalid template '{ template } '. Non-optional parameters or literal routes cannot appear after optional parameters.") ;
93
+ }
94
+
95
+ if ( string . Equals ( currentSegment . Value , nextSegment . Value , StringComparison . OrdinalIgnoreCase ) )
96
+ {
97
+ throw new InvalidOperationException (
98
+ $ "Invalid template '{ template } '. The parameter '{ currentSegment } ' appears multiple times.") ;
99
+ }
104
100
}
105
101
}
106
102
}
107
-
108
103
return new RouteTemplate ( template , templateSegments ) ;
109
104
}
110
105
}
0 commit comments