@@ -50,53 +50,75 @@ public static void AddSpaStaticFiles(
50
50
/// </summary>
51
51
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
52
52
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 )
53
64
{
54
65
if ( applicationBuilder == null )
55
66
{
56
67
throw new ArgumentNullException ( nameof ( applicationBuilder ) ) ;
57
68
}
58
69
70
+ if ( options == null )
71
+ {
72
+ throw new ArgumentNullException ( nameof ( options ) ) ;
73
+ }
74
+
59
75
UseSpaStaticFilesInternal ( applicationBuilder ,
60
- overrideFileProvider : null ,
76
+ staticFileOptions : options ,
61
77
allowFallbackOnServingWebRootFiles : false ) ;
62
78
}
63
79
64
80
internal static void UseSpaStaticFilesInternal (
65
81
this IApplicationBuilder app ,
66
- IFileProvider overrideFileProvider ,
82
+ StaticFileOptions staticFileOptions ,
67
83
bool allowFallbackOnServingWebRootFiles )
68
84
{
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
+ }
74
89
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 )
76
96
{
77
- app . UseStaticFiles ( new StaticFileOptions
97
+ var shouldServeStaticFiles = ShouldServeStaticFiles (
98
+ app ,
99
+ allowFallbackOnServingWebRootFiles ,
100
+ out var fileProviderOrDefault ) ;
101
+ if ( shouldServeStaticFiles )
78
102
{
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
+ }
81
111
}
112
+
113
+
114
+ app . UseStaticFiles ( staticFileOptions ) ;
82
115
}
83
116
84
117
private static bool ShouldServeStaticFiles (
85
118
IApplicationBuilder app ,
86
- IFileProvider overrideFileProvider ,
87
119
bool allowFallbackOnServingWebRootFiles ,
88
120
out IFileProvider fileProviderOrDefault )
89
121
{
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
-
100
122
var spaStaticFilesService = app . ApplicationServices . GetService < ISpaStaticFileProvider > ( ) ;
101
123
if ( spaStaticFilesService != null )
102
124
{
0 commit comments