Skip to content

Commit 43753d0

Browse files
author
Chris Martinez
committed
Improve performance of action selector
1 parent 5bf6447 commit 43753d0

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/Microsoft.AspNetCore.Mvc.Versioning/Abstractions/ActionDescriptorExtensions.cs

+5-23
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,30 @@
1313
[CLSCompliant( false )]
1414
public static class ActionDescriptorExtensions
1515
{
16-
const string VersionsAggregated = "MS_" + nameof( HasAggregatedVersions );
16+
const string VersionPolicyIsAppliedKey = "MS_" + nameof( VersionPolicyIsApplied );
1717

18-
static bool HasAggregatedVersions( this ActionDescriptor action ) => action.Properties.GetOrDefault( VersionsAggregated, false );
18+
static void VersionPolicyIsApplied( this ActionDescriptor action, bool value ) => action.Properties[VersionPolicyIsAppliedKey] = value;
1919

20-
static void HasAggregatedVersions( this ActionDescriptor action, bool value ) => action.Properties[VersionsAggregated] = value;
20+
internal static bool VersionPolicyIsApplied( this ActionDescriptor action ) => action.Properties.GetOrDefault( VersionPolicyIsAppliedKey, false );
2121

2222
internal static void AggregateAllVersions( this ActionDescriptor action, IEnumerable<ActionDescriptor> matchingActions )
2323
{
2424
Contract.Requires( action != null );
2525
Contract.Requires( matchingActions != null );
2626

27-
if ( action.HasAggregatedVersions() )
27+
if ( action.VersionPolicyIsApplied() )
2828
{
2929
return;
3030
}
3131

32-
action.HasAggregatedVersions( true );
32+
action.VersionPolicyIsApplied( true );
3333

3434
var model = action.GetProperty<ApiVersionModel>();
3535
Contract.Assume( model != null );
3636

3737
action.SetProperty( model.Aggregate( matchingActions.Select( a => a.GetProperty<ApiVersionModel>() ).Where( m => m != null ) ) );
3838
}
3939

40-
internal static void AggregateAllVersions( this ActionDescriptor action, ActionSelectionContext context )
41-
{
42-
Contract.Requires( action != null );
43-
Contract.Requires( context != null );
44-
45-
if ( action.HasAggregatedVersions() )
46-
{
47-
return;
48-
}
49-
50-
action.HasAggregatedVersions( true );
51-
52-
var model = action.GetProperty<ApiVersionModel>();
53-
Contract.Assume( model != null );
54-
55-
action.SetProperty( model.Aggregate( context.AllVersions ) );
56-
}
57-
5840
/// <summary>
5941
/// Returns a value indicating whether the provided action implicitly maps to the specified version.
6042
/// </summary>

src/Microsoft.AspNetCore.Mvc.Versioning/Versioning/ApiVersionActionSelector.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,24 @@ public virtual ActionDescriptor SelectBestCandidate( RouteContext context, IRead
108108
properties.ApiVersion = selectionContext.RequestedVersion;
109109
selectionResult.CandidateActions.AddRange( candidates );
110110

111-
if ( finalMatches?.Count > 0 )
111+
if ( finalMatches == null )
112+
{
113+
return null;
114+
}
115+
116+
if ( finalMatches.Count == 1 )
117+
{
118+
var selectedAction = finalMatches[0];
119+
120+
// note: short-circuit if the api version policy has already been applied to the match
121+
if ( selectedAction.VersionPolicyIsApplied() )
122+
{
123+
httpContext.ApiVersionProperties().ApiVersion = selectionContext.RequestedVersion;
124+
return selectedAction;
125+
}
126+
}
127+
128+
if ( finalMatches.Count > 0 )
112129
{
113130
var routeData = new RouteData( context.RouteData );
114131
selectionResult.MatchingActions.AddRange( finalMatches.Select( action => new ActionDescriptorMatch( action, routeData ) ) );

0 commit comments

Comments
 (0)