@@ -64,26 +64,30 @@ public SwaggerUIMiddleware(
64
64
public async Task Invoke ( HttpContext httpContext )
65
65
{
66
66
var httpMethod = httpContext . Request . Method ;
67
- var path = httpContext . Request . Path . Value ;
68
67
69
- var isGet = HttpMethods . IsGet ( httpMethod ) ;
70
-
71
- // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL
72
- if ( isGet && Regex . IsMatch ( path , $ "^/?{ Regex . Escape ( _options . RoutePrefix ) } /?$", RegexOptions . IgnoreCase ) )
68
+ if ( HttpMethods . IsGet ( httpMethod ) )
73
69
{
74
- // Use relative redirect to support proxy environments
75
- var relativeIndexUrl = string . IsNullOrEmpty ( path ) || path . EndsWith ( "/" )
76
- ? "index.html"
77
- : $ "{ path . Split ( '/' ) . Last ( ) } /index.html";
70
+ var path = httpContext . Request . Path . Value ;
78
71
79
- RespondWithRedirect ( httpContext . Response , relativeIndexUrl ) ;
80
- return ;
81
- }
72
+ // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL
73
+ if ( Regex . IsMatch ( path , $ "^/?{ Regex . Escape ( _options . RoutePrefix ) } /?$", RegexOptions . IgnoreCase ) )
74
+ {
75
+ // Use relative redirect to support proxy environments
76
+ var relativeIndexUrl = string . IsNullOrEmpty ( path ) || path . EndsWith ( "/" )
77
+ ? "index.html"
78
+ : $ "{ path . Split ( '/' ) . Last ( ) } /index.html";
82
79
83
- if ( isGet && Regex . IsMatch ( path , $ "^/{ Regex . Escape ( _options . RoutePrefix ) } /?index.html$", RegexOptions . IgnoreCase ) )
84
- {
85
- await RespondWithIndexHtml ( httpContext . Response ) ;
86
- return ;
80
+ RespondWithRedirect ( httpContext . Response , relativeIndexUrl ) ;
81
+ return ;
82
+ }
83
+
84
+ var match = Regex . Match ( path , $ "^/{ Regex . Escape ( _options . RoutePrefix ) } /?(index.(html|js))$", RegexOptions . IgnoreCase ) ;
85
+
86
+ if ( match . Success )
87
+ {
88
+ await RespondWithFile ( httpContext . Response , match . Groups [ 1 ] . Value ) ;
89
+ return ;
90
+ }
87
91
}
88
92
89
93
await _staticFileMiddleware . Invoke ( httpContext ) ;
@@ -110,23 +114,35 @@ private static void RespondWithRedirect(HttpResponse response, string location)
110
114
response . Headers [ "Location" ] = location ;
111
115
}
112
116
113
- private async Task RespondWithIndexHtml ( HttpResponse response )
117
+ private async Task RespondWithFile ( HttpResponse response , string fileName )
114
118
{
115
119
response . StatusCode = 200 ;
116
- response . ContentType = "text/html;charset=utf-8" ;
117
120
118
- using ( var stream = _options . IndexStream ( ) )
121
+ Stream stream ;
122
+
123
+ if ( fileName == "index.js" )
124
+ {
125
+ response . ContentType = "application/javascript;charset=utf-8" ;
126
+ stream = ResourceHelper . GetEmbeddedResource ( fileName ) ;
127
+ }
128
+ else
129
+ {
130
+ response . ContentType = "text/html;charset=utf-8" ;
131
+ stream = _options . IndexStream ( ) ;
132
+ }
133
+
134
+ using ( stream )
119
135
{
120
136
using var reader = new StreamReader ( stream ) ;
121
137
122
138
// Inject arguments before writing to response
123
- var htmlBuilder = new StringBuilder ( await reader . ReadToEndAsync ( ) ) ;
139
+ var content = new StringBuilder ( await reader . ReadToEndAsync ( ) ) ;
124
140
foreach ( var entry in GetIndexArguments ( ) )
125
141
{
126
- htmlBuilder . Replace ( entry . Key , entry . Value ) ;
142
+ content . Replace ( entry . Key , entry . Value ) ;
127
143
}
128
144
129
- await response . WriteAsync ( htmlBuilder . ToString ( ) , Encoding . UTF8 ) ;
145
+ await response . WriteAsync ( content . ToString ( ) , Encoding . UTF8 ) ;
130
146
}
131
147
}
132
148
0 commit comments