Skip to content

Commit ffb8b12

Browse files
Static files enable display /3 (#24252)
* Static files enable display /3 * Static files enable display /3
1 parent 1c2a0cd commit ffb8b12

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

aspnetcore/fundamentals/static-files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Directory browsing is disabled by default for security reasons. For more informa
116116

117117
Enable directory browsing with <xref:Microsoft.Extensions.DependencyInjection.DirectoryBrowserServiceExtensions.AddDirectoryBrowser%2A> and <xref:Microsoft.AspNetCore.Builder.DirectoryBrowserExtensions.UseDirectoryBrowser%2A>:
118118

119-
[!code-csharp[](~/fundamentals/static-files/samples/6.x/StaticFilesSample/Program.cs?name=snippet_db&highlight=8,22-27)]
119+
[!code-csharp[](~/fundamentals/static-files/samples/6.x/StaticFilesSample/Program.cs?name=snippet_db&highlight=9,23-37)]
120120

121121
<!-- Select RP Home > Directory browsing -->
122122
The preceding code allows directory browsing of the *wwwroot/images* folder using the URL `https://<hostname>/MyImages`, with links to each file and folder:

aspnetcore/fundamentals/static-files/samples/6.x/StaticFilesSample/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define FECTP // DEFAULT RR RH DB DF DF2 UFS UFS2 TREE FECTP NS
1+
#define DB // DEFAULT RR RH DB DF DF2 UFS UFS2 TREE FECTP NS
22
#if NEVER
33
#elif DEFAULT
44
#region snippet
@@ -94,6 +94,7 @@
9494
#endregion
9595
#elif DB // Directory Browsing
9696
#region snippet_db
97+
using Microsoft.AspNetCore.StaticFiles;
9798
using Microsoft.Extensions.FileProviders;
9899

99100
var builder = WebApplication.CreateBuilder(args);
@@ -115,11 +116,20 @@
115116

116117
app.UseStaticFiles();
117118

119+
var fileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, "images"));
120+
var requestPath = "/MyImages";
121+
122+
// Enable displaying browser links.
123+
app.UseStaticFiles(new StaticFileOptions
124+
{
125+
FileProvider = fileProvider,
126+
RequestPath = requestPath
127+
});
128+
118129
app.UseDirectoryBrowser(new DirectoryBrowserOptions
119130
{
120-
FileProvider = new PhysicalFileProvider(
121-
Path.Combine(builder.Environment.WebRootPath, "images")),
122-
RequestPath = "/MyImages"
131+
FileProvider = fileProvider,
132+
RequestPath = requestPath
123133
});
124134

125135
app.UseAuthorization();

0 commit comments

Comments
 (0)