@@ -190,56 +190,83 @@ private static bool HasWrongType(ParameterPair pair, out string correctType)
190190 foreach ( var constraint in constraints )
191191 {
192192 // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2#route-constraint-reference
193- switch ( constraint . Span . Text )
193+ if ( TryGetType ( constraint . Span . Text , out var type ) )
194194 {
195- case "bool" :
196- correctType = constraint . Span . Text ;
197- return parameter . Type != KnownSymbol . Boolean ;
198- case "decimal" :
199- correctType = constraint . Span . Text ;
200- return parameter . Type != KnownSymbol . Decimal ;
201- case "double" :
202- correctType = constraint . Span . Text ;
203- return parameter . Type != KnownSymbol . Double ;
204- case "float" :
205- correctType = constraint . Span . Text ;
206- return parameter . Type != KnownSymbol . Float ;
207- case "int" :
208- correctType = constraint . Span . Text ;
209- return parameter . Type != KnownSymbol . Int32 ;
210- case "long" :
211- correctType = constraint . Span . Text ;
212- return parameter . Type != KnownSymbol . Int64 ;
213- case "datetime" when parameter . Type != KnownSymbol . DateTime :
214- correctType = "System.DateTime" ;
215- return true ;
216- case "guid" when parameter . Type != KnownSymbol . Guid :
217- correctType = "System.Guid" ;
218- return true ;
219- case "alpha" when parameter . Type != KnownSymbol . String :
220- correctType = "string" ;
221- return true ;
222- case "required" :
223- continue ;
224- case string text when parameter . Type != KnownSymbol . String &&
225- ( text . StartsWith ( "regex(" , StringComparison . OrdinalIgnoreCase ) ||
226- text . StartsWith ( "length(" , StringComparison . OrdinalIgnoreCase ) ||
227- text . StartsWith ( "minlength(" , StringComparison . OrdinalIgnoreCase ) ||
228- text . StartsWith ( "maxlength(" , StringComparison . OrdinalIgnoreCase ) ) :
229- correctType = "string" ;
230- return true ;
231- case string text when parameter . Type != KnownSymbol . Int64 &&
232- ( text . StartsWith ( "min(" , StringComparison . OrdinalIgnoreCase ) ||
233- text . StartsWith ( "max(" , StringComparison . OrdinalIgnoreCase ) ||
234- text . StartsWith ( "range(" , StringComparison . OrdinalIgnoreCase ) ) :
235- correctType = "long" ;
236- return true ;
195+ correctType = parameter . Type == type ? null : type . Alias ?? type . FullName ;
196+ return correctType != null ;
237197 }
238198 }
239199 }
240200
241201 correctType = null ;
242202 return false ;
203+
204+ bool TryGetType ( string constraint , out QualifiedType type )
205+ {
206+ if ( constraint . Equals ( "bool" , StringComparison . Ordinal ) )
207+ {
208+ type = KnownSymbol . Boolean ;
209+ return true ;
210+ }
211+
212+ if ( constraint . Equals ( "decimal" , StringComparison . Ordinal ) )
213+ {
214+ type = KnownSymbol . Decimal ;
215+ return true ;
216+ }
217+
218+ if ( constraint . Equals ( "double" , StringComparison . Ordinal ) )
219+ {
220+ type = KnownSymbol . Double ;
221+ return true ;
222+ }
223+
224+ if ( constraint . Equals ( "float" , StringComparison . Ordinal ) )
225+ {
226+ type = KnownSymbol . Float ;
227+ return true ;
228+ }
229+
230+ if ( constraint . Equals ( "int" , StringComparison . Ordinal ) )
231+ {
232+ type = KnownSymbol . Int32 ;
233+ return true ;
234+ }
235+
236+ if ( constraint . Equals ( "long" , StringComparison . Ordinal ) ||
237+ constraint . StartsWith ( "min(" , StringComparison . OrdinalIgnoreCase ) ||
238+ constraint . StartsWith ( "max(" , StringComparison . OrdinalIgnoreCase ) ||
239+ constraint . StartsWith ( "range(" , StringComparison . OrdinalIgnoreCase ) )
240+ {
241+ type = KnownSymbol . Int64 ;
242+ return true ;
243+ }
244+
245+ if ( constraint . Equals ( "datetime" , StringComparison . Ordinal ) )
246+ {
247+ type = KnownSymbol . DateTime ;
248+ return true ;
249+ }
250+
251+ if ( constraint . Equals ( "guid" , StringComparison . Ordinal ) )
252+ {
253+ type = KnownSymbol . Guid ;
254+ return true ;
255+ }
256+
257+ if ( constraint . Equals ( "alpha" , StringComparison . OrdinalIgnoreCase ) ||
258+ constraint . StartsWith ( "regex(" , StringComparison . OrdinalIgnoreCase ) ||
259+ constraint . StartsWith ( "length(" , StringComparison . OrdinalIgnoreCase ) ||
260+ constraint . StartsWith ( "minlength(" , StringComparison . OrdinalIgnoreCase ) ||
261+ constraint . StartsWith ( "maxlength(" , StringComparison . OrdinalIgnoreCase ) )
262+ {
263+ type = KnownSymbol . String ;
264+ return true ;
265+ }
266+
267+ type = null ;
268+ return false ;
269+ }
243270 }
244271
245272 private static bool HasWrongSyntax ( PathSegment segment , out Location location , out string correctSyntax )
0 commit comments