@@ -17,24 +17,31 @@ public static class OpenApiUiMiddlewareExtensions
1717 /// This middleware serves an embedded OpenAPI documentation interface.
1818 /// </summary>
1919 /// <param name="app">The application builder instance.</param>
20- /// <param name="openApiSpecPath ">The path to the OpenAPI specification JSON file. Defaults to "/swagger/v1/swagger.json" .</param>
20+ /// <param name="configuration ">The OpenAPI UI configuration options .</param>
2121 /// <returns>The application builder instance for method chaining.</returns>
22- public static IApplicationBuilder UseOpenApiUi ( this IApplicationBuilder app , string openApiSpecPath = "/swagger/v1/swagger.json" )
22+ public static IApplicationBuilder UseOpenApiUi ( this IApplicationBuilder app , OpenApiUiConfiguration configuration )
2323 {
24- var assembly = Assembly . GetExecutingAssembly ( ) ; var embeddedProvider = new EmbeddedFileProvider ( assembly , "JakubKozera.OpenApiUi.openapi-ui" ) ;
24+ if ( configuration == null )
25+ throw new ArgumentNullException ( nameof ( configuration ) ) ;
26+
27+ var assembly = Assembly . GetExecutingAssembly ( ) ;
28+ var embeddedProvider = new EmbeddedFileProvider ( assembly , "JakubKozera.OpenApiUi.openapi-ui" ) ;
29+
30+ // Use the configurable UI path for serving static files
31+ var requestPath = $ "/{ configuration . OpenApiUiPath . TrimStart ( '/' ) } ";
2532
2633 app . UseStaticFiles ( new StaticFileOptions
2734 {
2835 FileProvider = embeddedProvider ,
29- RequestPath = "/openapi-ui" ,
36+ RequestPath = requestPath ,
3037 ServeUnknownFileTypes = true
3138 } ) ;
3239
3340 app . Use ( async ( context , next ) =>
3441 {
35- var requestPath = context . Request . Path . Value ;
42+ var currentPath = context . Request . Path . Value ;
3643
37- if ( context . Request . Path == "/openapi-ui" || context . Request . Path == "/openapi-ui /")
44+ if ( context . Request . Path == requestPath || context . Request . Path == $ " { requestPath } /")
3845 {
3946 var fileInfo = embeddedProvider . GetFileInfo ( "index.html" ) ;
4047
@@ -47,12 +54,13 @@ public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app, str
4754 var content = await reader . ReadToEndAsync ( ) ;
4855
4956 // Replace the placeholder with the actual OpenAPI spec path
50- var originalContent = content ;
51- content = content . Replace ( "#swagger_path#" , openApiSpecPath ) ;
57+ content = content . Replace ( "#swagger_path#" , configuration . OpenApiSpecPath ) ;
5258
53- content = content . Replace ( "bundle.css" , "openapi-ui/bundle.css" ) ;
54- content = content . Replace ( "bundle.js" , "openapi-ui/bundle.js" ) ;
55- content = content . Replace ( "openapi-ui.png" , "openapi-ui/openapi-ui.png" ) ;
59+ // Update resource paths to use the configurable UI path
60+ var uiPath = configuration . OpenApiUiPath . TrimStart ( '/' ) ;
61+ content = content . Replace ( "bundle.css" , $ "{ uiPath } /bundle.css") ;
62+ content = content . Replace ( "bundle.js" , $ "{ uiPath } /bundle.js") ;
63+ content = content . Replace ( "openapi-ui.png" , $ "{ uiPath } /openapi-ui.png") ;
5664
5765 context . Response . ContentType = "text/html" ;
5866 await context . Response . WriteAsync ( content ) ;
@@ -79,5 +87,14 @@ public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app, str
7987
8088 return app ;
8189 }
90+
91+ /// <summary>
92+ /// Adds OpenAPI UI middleware to the ASP.NET Core application pipeline with default configuration.
93+ /// This middleware serves an embedded OpenAPI documentation interface.
94+ /// </summary>
95+ /// <param name="app">The application builder instance.</param>
96+ /// <returns>The application builder instance for method chaining.</returns>
97+ public static IApplicationBuilder UseOpenApiUi ( this IApplicationBuilder app )
98+ => UseOpenApiUi ( app , new OpenApiUiConfiguration ( ) ) ;
8299 }
83100}
0 commit comments