1+ using System . Collections . Generic ;
12using System . IO ;
23using System . Linq ;
34using System . Net ;
67using Microsoft . AspNetCore . Builder ;
78using Microsoft . AspNetCore . Hosting ;
89using Microsoft . AspNetCore . Hosting . Server . Features ;
10+ using Microsoft . AspNetCore . StaticFiles ;
911using Microsoft . Extensions . DependencyInjection ;
1012using Microsoft . Extensions . FileProviders ;
1113using Microsoft . Extensions . Logging ;
@@ -16,9 +18,10 @@ public sealed class HostedContent : IWebviewContent
1618 {
1719 private readonly IWebHost _webHost ;
1820
19- public HostedContent ( int port = 0 , bool activateLog = false )
21+ public HostedContent ( int port = 0 , bool activateLog = false , IDictionary < string , string > additionalMimeTypes = null )
2022 {
2123 _webHost = WebHost . CreateDefaultBuilder ( )
24+ . ConfigureServices ( s => s . AddSingleton ( x => new StartupParameters ( ) { AdditionalMimeTypes = additionalMimeTypes } ) )
2225 . UseStartup < Startup > ( )
2326 . UseKestrel ( options => options . Listen ( IPAddress . Loopback , port ) )
2427 . ConfigureLogging ( ( logger ) => { if ( ! activateLog ) logger . ClearProviders ( ) ; } )
@@ -37,16 +40,29 @@ public string ToWebviewUrl()
3740
3841 internal sealed class Startup
3942 {
43+ private StartupParameters startupParameters ;
44+ public Startup ( StartupParameters startupParameters )
45+ {
46+ this . startupParameters = startupParameters ;
47+ }
4048 public void ConfigureServices ( IServiceCollection services ) { }
4149 public void Configure ( IApplicationBuilder app , IHostingEnvironment env )
4250 {
4351 var workingDirectory = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
44- app . UseFileServer ( new FileServerOptions
52+ var fileServerOptions = new FileServerOptions
4553 {
4654 FileProvider = new PhysicalFileProvider ( Path . Combine ( workingDirectory , "app" ) ) ,
4755 RequestPath = "" ,
48- EnableDirectoryBrowsing = true
49- } ) ;
56+ EnableDirectoryBrowsing = true ,
57+ } ;
58+ if ( startupParameters . AdditionalMimeTypes != null )
59+ {
60+ var extensionProvider = new FileExtensionContentTypeProvider ( ) ;
61+ foreach ( var mimeType in startupParameters . AdditionalMimeTypes )
62+ extensionProvider . Mappings . Add ( mimeType ) ;
63+ fileServerOptions . StaticFileOptions . ContentTypeProvider = extensionProvider ;
64+ }
65+ app . UseFileServer ( fileServerOptions ) ;
5066 }
5167 }
5268}
0 commit comments