环境:Net6
string dirPath = "C:\\Users\\15298\\Pictures";
var fileProvider = new PhysicalFileProvider(dirPath);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = fileProvider,
RequestPath = new PathString(("/files"))
});
// 开启目录浏览功能 当访问 files 的时候 会把 {{ dirPath }} 目录列出来
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
// 文件提供程序
FileProvider = fileProvider,
/// 指定请求地址
RequestPath = new PathString("/files")
});
完整 Program.cs
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
string dirPath = "C:\\Users\\15298\\Pictures";
var fileProvider = new PhysicalFileProvider(dirPath);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = fileProvider,
RequestPath = new PathString(("/files"))
});
// 开启目录浏览功能 当访问 files 的时候 会把 {{ dirPath }} 目录列出来
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
// 文件提供程序
FileProvider = fileProvider,
/// 指定请求地址
RequestPath = new PathString("/files")
});
app.UseAuthorization();
app.MapControllers();
app.Run();
}
http://IP:Port/files 访问目录浏览