Skip to content

Commit ce3a748

Browse files
committed
Use ApplicationName instead of "Map" if no declaring type
1 parent 31729d2 commit ce3a748

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,32 @@
1616
using Microsoft.AspNetCore.Routing;
1717
using Microsoft.AspNetCore.Routing.Patterns;
1818
using Microsoft.Extensions.DependencyInjection;
19+
using Microsoft.Extensions.Hosting;
1920
using Microsoft.Extensions.Internal;
2021

2122
namespace Microsoft.AspNetCore.Mvc.ApiExplorer
2223
{
2324
internal class EndpointMetadataApiDescriptionProvider : IApiDescriptionProvider
2425
{
2526
private readonly EndpointDataSource _endpointDataSource;
27+
private readonly IHostEnvironment _environment;
2628
private readonly IServiceProviderIsService? _serviceProviderIsService;
2729

2830
// Executes before MVC's DefaultApiDescriptionProvider and GrpcHttpApiDescriptionProvider for no particular reason :D
2931
public int Order => -1100;
3032

31-
public EndpointMetadataApiDescriptionProvider(EndpointDataSource endpointDataSource)
32-
: this(endpointDataSource, null)
33+
public EndpointMetadataApiDescriptionProvider(EndpointDataSource endpointDataSource, IHostEnvironment environment)
34+
: this(endpointDataSource, environment, null)
3335
{
3436
}
3537

36-
public EndpointMetadataApiDescriptionProvider(EndpointDataSource endpointDataSource, IServiceProviderIsService? serviceProviderIsService)
38+
public EndpointMetadataApiDescriptionProvider(
39+
EndpointDataSource endpointDataSource,
40+
IHostEnvironment environment,
41+
IServiceProviderIsService? serviceProviderIsService)
3742
{
3843
_endpointDataSource = endpointDataSource;
44+
_environment = environment;
3945
_serviceProviderIsService = serviceProviderIsService;
4046
}
4147

@@ -76,7 +82,7 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
7682
{
7783
// If the declaring type is null or compiler-generated (e.g. lambdas),
7884
// group the methods under a "Map" controller.
79-
controllerName = "Map";
85+
controllerName = _environment.ApplicationName;
8086
}
8187

8288
var apiDescription = new ApiDescription

src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using Microsoft.AspNetCore.Routing;
1313
using Microsoft.AspNetCore.Routing.Patterns;
1414
using Microsoft.Extensions.DependencyInjection;
15+
using Microsoft.Extensions.FileProviders;
16+
using Microsoft.Extensions.Hosting;
1517
using Xunit;
1618

1719
namespace Microsoft.AspNetCore.Mvc.ApiExplorer
@@ -44,11 +46,11 @@ public void UsesDeclaringTypeAsControllerName()
4446
}
4547

4648
[Fact]
47-
public void UsesMapAsControllerNameIfNoDeclaringType()
49+
public void UsesApplicationNameAsControllerNameIfNoDeclaringType()
4850
{
4951
var apiDescription = GetApiDescription(() => { });
5052

51-
Assert.Equal("Map", apiDescription.ActionDescriptor.RouteValues["controller"]);
53+
Assert.Equal(nameof(EndpointMetadataApiDescriptionProviderTest), apiDescription.ActionDescriptor.RouteValues["controller"]);
5254
}
5355

5456
[Fact]
@@ -324,8 +326,12 @@ private IList<ApiDescription> GetApiDescriptions(
324326

325327
var endpoint = new RouteEndpoint(httpContext => Task.CompletedTask, routePattern, 0, endpointMetadata, null);
326328
var endpointDataSource = new DefaultEndpointDataSource(endpoint);
329+
var hostEnvironment = new HostEnvironment
330+
{
331+
ApplicationName = nameof(EndpointMetadataApiDescriptionProviderTest)
332+
};
327333

328-
var provider = new EndpointMetadataApiDescriptionProvider(endpointDataSource, new ServiceProviderIsService());
334+
var provider = new EndpointMetadataApiDescriptionProvider(endpointDataSource, hostEnvironment, new ServiceProviderIsService());
329335

330336
provider.OnProvidersExecuting(context);
331337
provider.OnProvidersExecuted(context);
@@ -360,5 +366,13 @@ private class ServiceProviderIsService : IServiceProviderIsService
360366
{
361367
public bool IsService(Type serviceType) => serviceType == typeof(IInferredServiceInterface);
362368
}
369+
370+
private class HostEnvironment : IHostEnvironment
371+
{
372+
public string EnvironmentName { get; set; }
373+
public string ApplicationName { get; set; }
374+
public string ContentRootPath { get; set; }
375+
public IFileProvider ContentRootFileProvider { get; set; }
376+
}
363377
}
364378
}

0 commit comments

Comments
 (0)