@@ -3,6 +3,8 @@ namespace ConfigurationDebugViewEndpoint.Extensions
3
3
using System ;
4
4
using Microsoft . AspNetCore . Builder ;
5
5
using Microsoft . AspNetCore . Routing ;
6
+ using Microsoft . Extensions . DependencyInjection ;
7
+ using Microsoft . Extensions . Hosting ;
6
8
7
9
/// <summary>
8
10
/// Provides extension methods for <see cref="IEndpointRouteBuilder"/> to add routes.
@@ -14,24 +16,36 @@ public static class EndpointRouteBuilderExtensions
14
16
/// </summary>
15
17
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add endpoint to.</param>
16
18
/// <param name="pattern">The URL pattern of the endpoint.</param>
19
+ /// <param name="optionsDelegate"></param>
17
20
/// <returns>A route for the endpoint.</returns>
18
- public static IEndpointConventionBuilder MapConfigurationDebugView (
21
+ public static IEndpointConventionBuilder ? MapConfigurationDebugView (
19
22
this IEndpointRouteBuilder endpoints ,
20
- string pattern = "config" )
23
+ string pattern = "config" ,
24
+ Action < ConfigurationDebugViewOptions > ? optionsDelegate = default )
21
25
{
22
26
if ( endpoints == null )
23
27
{
24
28
throw new ArgumentNullException ( nameof ( endpoints ) ) ;
25
29
}
26
30
27
- return MapConfigurationDebugViewCore ( endpoints , pattern ) ;
31
+ var options = new ConfigurationDebugViewOptions ( ) ;
32
+ optionsDelegate ? . Invoke ( options ) ;
33
+
34
+ return MapConfigurationDebugViewCore ( endpoints , pattern , options ) ;
28
35
}
29
36
30
- private static IEndpointConventionBuilder MapConfigurationDebugViewCore (
37
+ private static IEndpointConventionBuilder ? MapConfigurationDebugViewCore (
31
38
IEndpointRouteBuilder endpoints ,
32
- string pattern )
39
+ string pattern , ConfigurationDebugViewOptions options )
33
40
{
34
- var pipeline = endpoints . CreateApplicationBuilder ( )
41
+ var environment = endpoints . ServiceProvider . GetRequiredService < IHostEnvironment > ( ) ;
42
+ var builder = endpoints . CreateApplicationBuilder ( ) ;
43
+
44
+ if ( options . AllowDevelopmentOnly && ! environment . IsDevelopment ( ) )
45
+ {
46
+ return null ;
47
+ }
48
+ var pipeline = builder
35
49
. UseMiddleware < ConfigurationDebugViewMiddleware > ( )
36
50
. Build ( ) ;
37
51
0 commit comments