@@ -54,6 +54,40 @@ private bool ShouldHandleUIStaticFilesRequest(HttpContext context)
5454 return false ;
5555 }
5656
57+ /// <summary>
58+ /// 为了适配 pathbase ,index.html 注入的 js css ,需要使用相对路径,所以要去除 /
59+ /// </summary>
60+ /// <param name="filePath"></param>
61+ /// <returns></returns>
62+ private async Task RewriteIndexHtml ( string filePath )
63+ {
64+ var rows = await File . ReadAllLinesAsync ( filePath ) ;
65+ for ( int i = 0 ; i < rows . Length ; i ++ )
66+ {
67+ var line = rows [ i ] ;
68+ if ( line . Contains ( "window.resourceBaseUrl = \" /\" " ) )
69+ {
70+ if ( ! string . IsNullOrWhiteSpace ( Appsettings . PathBase ) )
71+ {
72+ line = line . Replace ( "/" , $ "{ Appsettings . PathBase } /") ;
73+ rows [ i ] = line ;
74+ }
75+ }
76+ if ( line . Contains ( "<link rel=\" stylesheet\" href=\" /umi." ) )
77+ {
78+ line = line . Replace ( "/umi." , "umi." ) ;
79+ rows [ i ] = line ;
80+ }
81+ if ( line . Contains ( "<script src=\" /umi." ) )
82+ {
83+ line = line . Replace ( "/umi." , "umi." ) ;
84+ rows [ i ] = line ;
85+ }
86+
87+ }
88+ await File . WriteAllLinesAsync ( filePath , rows ) ;
89+ }
90+
5791 private static readonly string UiDirectory = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "wwwroot/ui" ) ;
5892 public async Task Invoke ( HttpContext context )
5993 {
@@ -93,6 +127,11 @@ public async Task Invoke(HttpContext context)
93127 }
94128 else
95129 {
130+ if ( filePath . EndsWith ( "index.html" ) )
131+ {
132+ await RewriteIndexHtml ( filePath ) ;
133+ }
134+
96135 var fileData = await File . ReadAllBytesAsync ( filePath ) ; //read file bytes
97136 var lastModified = File . GetLastWriteTime ( filePath ) ;
98137 var extType = Path . GetExtension ( filePath ) ;
0 commit comments