builder.Services.AddDirectoryBrowser();
//打开日志文件目录浏览
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "logs")),
RequestPath = "/logs",
EnableDirectoryBrowsing = true,
StaticFileOptions =
{
ServeUnknownFileTypes = true,
DefaultContentType = "application/octet-stream",
OnPrepareResponse = ctx =>
{
var filePath = ctx.File.PhysicalPath;
// 使用 Path.GetFileName 和 StartsWith + EndsWith 来确保只匹配日志文件
if (Path.GetFileName(filePath).StartsWith("log-", StringComparison.OrdinalIgnoreCase) && filePath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
ctx.Context.Response.Headers["Content-Disposition"] = "attachment; filename=\"" + Path.GetFileName(filePath) + "\"";
}
}
}
});
访问 ip+/logs/logs
标签:GetFileName,core,logs,filePath,浏览,ctx,Path,net From: https://www.cnblogs.com/kaixuan2316640647/p/18372041