diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index bd653faf557..3cada3bf462 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -15,6 +15,7 @@ using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.Security; using Volo.Abp.AspNetCore.Security.Claims; +using Volo.Abp.AspNetCore.StaticFiles; using Volo.Abp.AspNetCore.Tracing; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.AspNetCore.VirtualFileSystem; @@ -126,6 +127,38 @@ public static IApplicationBuilder UseDynamicClaims(this IApplicationBuilder app) return app.UseMiddleware(); } + /// + /// Configures the application to serve static files that match the specified filename patterns with the WebRootFileProvider of the application. + /// + /// The used to configure the application pipeline. + /// The file name patterns to include when serving static files (e.g., "appsettings*.json"). + /// Supports glob patterns. See Glob patterns documentation. + /// + /// The instance. + public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, params string[] includeFileNamePatterns) + { + return UseStaticFilesForPatterns(app, includeFileNamePatterns, app.ApplicationServices.GetRequiredService().WebRootFileProvider); + } + + /// + /// Configures the application to serve static files that match the specified filename patterns with the specified file provider. + /// + /// The used to configure the application pipeline. + /// The file name patterns to include when serving static files (e.g., "appsettings*.json"). + /// Supports glob patterns. See Glob patterns documentation. + /// + /// The + /// The instance. + public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, string[] includeFileNamePatterns, IFileProvider fileProvider) + { + app.UseStaticFiles(new StaticFileOptions + { + FileProvider = new AbpStaticFileProvider(includeFileNamePatterns, fileProvider) + }); + + return app; + } + /// /// MapAbpStaticAssets is used to serve the files from the abp virtual file system embedded resources(js/css) and call the MapStaticAssets. /// diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs new file mode 100644 index 00000000000..f7893932f2c --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.FileSystemGlobbing; +using Microsoft.Extensions.Primitives; + +namespace Volo.Abp.AspNetCore.StaticFiles; + +public class AbpStaticFileProvider : IFileProvider +{ + private readonly Matcher _matcher; + private readonly IFileProvider _fileProvider; + + /// The file provider to be used to get the files. + /// The file name patterns to include when serving static files (e.g., "appsettings*.json"). + /// Supports glob patterns. See Glob patterns documentation. + /// + public AbpStaticFileProvider(IReadOnlyList fileNamePatterns, IFileProvider fileProvider) + { + _fileProvider = fileProvider; + _matcher = new Matcher(StringComparison.OrdinalIgnoreCase); + _matcher.AddIncludePatterns(fileNamePatterns); + } + + public IDirectoryContents GetDirectoryContents(string subpath) + { + return new NotFoundDirectoryContents(); + } + + public IFileInfo GetFileInfo(string subpath) + { + return _matcher.Match(Path.GetFileName(subpath)).HasMatches ? + _fileProvider.GetFileInfo(subpath) : + new NotFoundFileInfo(subpath); + } + + public IChangeToken Watch(string filter) + { + return NullChangeToken.Singleton; + } +} \ No newline at end of file