@@ -50,53 +50,75 @@ public static void AddSpaStaticFiles(
5050 /// </summary>
5151 /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
5252 public static void UseSpaStaticFiles ( this IApplicationBuilder applicationBuilder )
53+ {
54+ UseSpaStaticFiles ( applicationBuilder , new StaticFileOptions ( ) ) ;
55+ }
56+
57+ /// <summary>
58+ /// Configures the application to serve static files for a Single Page Application (SPA).
59+ /// The files will be located using the registered <see cref="ISpaStaticFileProvider"/> service.
60+ /// </summary>
61+ /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
62+ /// <param name="options">Specifies options for serving the static files.</param>
63+ public static void UseSpaStaticFiles ( this IApplicationBuilder applicationBuilder , StaticFileOptions options )
5364 {
5465 if ( applicationBuilder == null )
5566 {
5667 throw new ArgumentNullException ( nameof ( applicationBuilder ) ) ;
5768 }
5869
70+ if ( options == null )
71+ {
72+ throw new ArgumentNullException ( nameof ( options ) ) ;
73+ }
74+
5975 UseSpaStaticFilesInternal ( applicationBuilder ,
60- overrideFileProvider : null ,
76+ staticFileOptions : options ,
6177 allowFallbackOnServingWebRootFiles : false ) ;
6278 }
6379
6480 internal static void UseSpaStaticFilesInternal (
6581 this IApplicationBuilder app ,
66- IFileProvider overrideFileProvider ,
82+ StaticFileOptions staticFileOptions ,
6783 bool allowFallbackOnServingWebRootFiles )
6884 {
69- var shouldServeStaticFiles = ShouldServeStaticFiles (
70- app ,
71- overrideFileProvider ,
72- allowFallbackOnServingWebRootFiles ,
73- out var fileProviderOrDefault ) ;
85+ if ( staticFileOptions == null )
86+ {
87+ throw new ArgumentNullException ( nameof ( staticFileOptions ) ) ;
88+ }
7489
75- if ( shouldServeStaticFiles )
90+ // If the file provider was explicitly supplied, that takes precedence over any other
91+ // configured file provider. This is most useful if the application hosts multiple SPAs
92+ // (via multiple calls to UseSpa()), so each needs to serve its own separate static files
93+ // instead of using AddSpaStaticFiles/UseSpaStaticFiles.
94+ // But if no file provider was specified, try to get one from the DI config.
95+ if ( staticFileOptions . FileProvider == null )
7696 {
77- app . UseStaticFiles ( new StaticFileOptions
97+ var shouldServeStaticFiles = ShouldServeStaticFiles (
98+ app ,
99+ allowFallbackOnServingWebRootFiles ,
100+ out var fileProviderOrDefault ) ;
101+ if ( shouldServeStaticFiles )
78102 {
79- FileProvider = fileProviderOrDefault
80- } ) ;
103+ staticFileOptions . FileProvider = fileProviderOrDefault ;
104+ }
105+ else
106+ {
107+ // The registered ISpaStaticFileProvider says we shouldn't
108+ // serve static files
109+ return ;
110+ }
81111 }
112+
113+
114+ app . UseStaticFiles ( staticFileOptions ) ;
82115 }
83116
84117 private static bool ShouldServeStaticFiles (
85118 IApplicationBuilder app ,
86- IFileProvider overrideFileProvider ,
87119 bool allowFallbackOnServingWebRootFiles ,
88120 out IFileProvider fileProviderOrDefault )
89121 {
90- if ( overrideFileProvider != null )
91- {
92- // If the file provider was explicitly supplied, that takes precedence over any other
93- // configured file provider. This is most useful if the application hosts multiple SPAs
94- // (via multiple calls to UseSpa()), so each needs to serve its own separate static files
95- // instead of using AddSpaStaticFiles/UseSpaStaticFiles.
96- fileProviderOrDefault = overrideFileProvider ;
97- return true ;
98- }
99-
100122 var spaStaticFilesService = app . ApplicationServices . GetService < ISpaStaticFileProvider > ( ) ;
101123 if ( spaStaticFilesService != null )
102124 {
0 commit comments