Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 8a8db23

Browse files
Allow explicit configuration of StaticFileOptions in new SPA APIs. Fixes #1424.
1 parent 26d666d commit 8a8db23

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

src/Microsoft.AspNetCore.SpaServices.Extensions/SpaDefaultPageMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void Attach(ISpaBuilder spaBuilder)
3131
// Developers who need to host more than one SPA with distinct default pages can
3232
// override the file provider
3333
app.UseSpaStaticFilesInternal(
34-
overrideFileProvider: options.DefaultPageFileProvider,
34+
options.DefaultPageStaticFileOptions ?? new StaticFileOptions(),
3535
allowFallbackOnServingWebRootFiles: true);
3636

3737
// If the default file didn't get served as a static file (usually because it was not

src/Microsoft.AspNetCore.SpaServices.Extensions/SpaOptions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using Microsoft.AspNetCore.Builder;
45
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.Extensions.FileProviders;
@@ -29,7 +30,7 @@ public SpaOptions()
2930
internal SpaOptions(SpaOptions copyFromOptions)
3031
{
3132
_defaultPage = copyFromOptions.DefaultPage;
32-
DefaultPageFileProvider = copyFromOptions.DefaultPageFileProvider;
33+
DefaultPageStaticFileOptions = copyFromOptions.DefaultPageStaticFileOptions;
3334
SourcePath = copyFromOptions.SourcePath;
3435
}
3536

@@ -52,14 +53,14 @@ public PathString DefaultPage
5253
}
5354

5455
/// <summary>
55-
/// Gets or sets the <see cref="IFileProvider"/> that supplies content
56+
/// Gets or sets the <see cref="StaticFileOptions"/> that supplies content
5657
/// for serving the SPA's default page.
5758
///
5859
/// If not set, a default file provider will read files from the
5960
/// <see cref="IHostingEnvironment.WebRootPath"/>, which by default is
6061
/// the <c>wwwroot</c> directory.
6162
/// </summary>
62-
public IFileProvider DefaultPageFileProvider { get; set; }
63+
public StaticFileOptions DefaultPageStaticFileOptions { get; set; }
6364

6465
/// <summary>
6566
/// Gets or sets the path, relative to the application working directory,

src/Microsoft.AspNetCore.SpaServices.Extensions/StaticFiles/SpaStaticFilesExtensions.cs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)