Skip to content

Commit c32a7eb

Browse files
Use API name for [controller] route value in Minimal APIs. Relates to #812
1 parent 2fc33e3 commit c32a7eb

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/VersionedApiDescriptionProvider.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public VersionedApiDescriptionProvider(
3232
IModelMetadataProvider modelMetadataProvider,
3333
IOptions<ApiExplorerOptions> options )
3434
{
35-
SunsetPolicyManager = sunsetPolicyManager ?? throw new ArgumentNullException( nameof( sunsetPolicyManager ) );
36-
ModelMetadataProvider = modelMetadataProvider ?? throw new ArgumentNullException( nameof( modelMetadataProvider ) );
37-
this.options = options ?? throw new ArgumentNullException( nameof( options ) );
35+
SunsetPolicyManager = sunsetPolicyManager;
36+
ModelMetadataProvider = modelMetadataProvider;
37+
this.options = options;
3838
}
3939

4040
/// <summary>
@@ -128,6 +128,8 @@ public virtual void OnProvidersExecuted( ApiDescriptionProviderContext context )
128128
continue;
129129
}
130130

131+
TryUpdateControllerRouteValueForMinimalApi( result );
132+
131133
var groupResult = result.Clone();
132134
var metadata = action.GetApiVersionMetadata();
133135

@@ -179,7 +181,7 @@ private static bool IsUnversioned( ActionDescriptor action )
179181

180182
for ( var i = 0; i < endpointMetadata.Count; i++ )
181183
{
182-
if ( endpointMetadata[i] is ApiVersionMetadata metadata )
184+
if ( endpointMetadata[i] is ApiVersionMetadata )
183185
{
184186
return false;
185187
}
@@ -188,6 +190,30 @@ private static bool IsUnversioned( ActionDescriptor action )
188190
return true;
189191
}
190192

193+
private static void TryUpdateControllerRouteValueForMinimalApi( ApiDescription description )
194+
{
195+
var action = description.ActionDescriptor;
196+
197+
if ( action is ControllerActionDescriptor )
198+
{
199+
return;
200+
}
201+
202+
var routeValues = action.RouteValues;
203+
204+
if ( !routeValues.ContainsKey( "controller" ) )
205+
{
206+
return;
207+
}
208+
209+
var metadata = action.GetApiVersionMetadata();
210+
211+
if ( !string.IsNullOrEmpty( metadata.Name ) )
212+
{
213+
routeValues["controller"] = metadata.Name;
214+
}
215+
}
216+
191217
private IEnumerable<ApiVersion> FlattenApiVersions( IList<ApiDescription> descriptions )
192218
{
193219
var versions = default( SortedSet<ApiVersion> );

0 commit comments

Comments
 (0)