@@ -5,6 +5,7 @@ namespace Asp.Versioning;
55using Microsoft . AspNetCore . Http ;
66using Microsoft . Extensions . DependencyInjection ;
77using System . Globalization ;
8+ using System . Runtime . CompilerServices ;
89
910/// <summary>
1011/// Represents the API versioning feature.
@@ -33,11 +34,10 @@ public IReadOnlyList<string> RawRequestedApiVersions
3334 {
3435 if ( rawApiVersions is null )
3536 {
36- var reader =
37- context . RequestServices . GetService < IApiVersionReader > ( ) ??
38- ApiVersionReader . Combine (
39- new QueryStringApiVersionReader ( ) ,
40- new UrlSegmentApiVersionReader ( ) ) ;
37+ var reader = context . RequestServices . GetService < IApiVersionReader > ( )
38+ ?? ApiVersionReader . Combine (
39+ new QueryStringApiVersionReader ( ) ,
40+ new UrlSegmentApiVersionReader ( ) ) ;
4141
4242 rawApiVersions = reader . Read ( context . Request ) ;
4343 }
@@ -58,11 +58,7 @@ public string? RawRequestedApiVersion
5858 {
5959 0 => default ,
6060 1 => values [ 0 ] ,
61- #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations; existing behavior via IApiVersionReader.Read
62- _ => throw new AmbiguousApiVersionException (
63- string . Format ( CultureInfo . CurrentCulture , CommonSR . MultipleDifferentApiVersionsRequested , string . Join ( ", " , values ) ) ,
64- values ) ,
65- #pragma warning restore CA1065
61+ _ => throw NewAmbiguousApiVersionException ( values ) ,
6662 } ;
6763 }
6864 set
@@ -88,7 +84,8 @@ public ApiVersion? RequestedApiVersion
8884 return apiVersion ;
8985 }
9086
91- var parser = context . RequestServices . GetRequiredService < IApiVersionParser > ( ) ;
87+ var parser = context . RequestServices . GetService < IApiVersionParser > ( )
88+ ?? ApiVersionParser . Default ;
9289
9390 try
9491 {
@@ -105,10 +102,20 @@ public ApiVersion? RequestedApiVersion
105102 {
106103 apiVersion = value ;
107104
108- if ( apiVersion is not null && ( rawApiVersions is null || rawApiVersions . Count == 0 ) )
105+ if ( apiVersion is not null &&
106+ ( rawApiVersions is null || rawApiVersions . Count == 0 ) )
109107 {
110108 rawApiVersions = new [ ] { apiVersion . ToString ( ) } ;
111109 }
112110 }
113111 }
112+
113+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
114+ private static AmbiguousApiVersionException NewAmbiguousApiVersionException ( IReadOnlyList < string > values ) =>
115+ new (
116+ string . Format (
117+ CultureInfo . CurrentCulture ,
118+ CommonSR . MultipleDifferentApiVersionsRequested ,
119+ string . Join ( ", " , values . ToArray ( ) , 0 , values . Count ) ) ,
120+ values ) ;
114121}
0 commit comments