Reputation: 342
I want to show the directory content, so that users can browse it and click files to download them.
Is there a solution in ASP .Net Core to do that?
Upvotes: 1
Views: 1120
Reputation: 4913
The fastest solution is to add a middleware with app.UseDirectoryBrowser();
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseDirectoryBrowser();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
}
It gives the following result :
Moreover, a request path can be added so that it has to be included in the request.
app.UseDirectoryBrowser(requestPath:"/data");
i.e. : https://localhost:44386/data/
Last : file provider and file formatter can be provided :
app.UseDirectoryBrowser(options:
new DirectoryBrowserOptions(
new SharedOptions()
{
// the IFileProvider class is a way to provide the files to be displayed
FileProvider = new MyFileProvider()
}
)
{
// The IFileFormatter implementation is a way to customize presentation of the directory
RequestPath = "/data2",
Formatter = new MyFileFormatter()
}
);
Upvotes: 1