aspnet .core 网站默认不支持文件下载

将txt文件和rar文件都放到 wwwroot 目录下,会发现前者可以正常下载而后者不行。其实是需要在初始化的地方进行设置:

builder.WebHost.UseKestrel(option =>
{
option.ListenAnyIP(config.Port);
});

WebApplication app = builder.Build();

var httpContextAccessor = app.Services.GetRequiredService<IHttpContextAccessor>();
xHttpContext.Configure(httpContextAccessor);

//app.UseStaticFiles().UseRouting().xUseMyCustomMiddleware();
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "application/octet-stream",

//ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
//{
// 【西西 2024-08-06 140916】这种方式就是一个一个的配,可以动态化。也可以像上面那样一刀切。
// { ".7z", "" }
//}),
}).UseRouting().xUseMyCustomMiddleware();

InitRoutes(app);
BizGlobal.Init();

app.Run();

 

posted @ 2024-08-06 14:10  oct  阅读(39)  评论(0)    收藏  举报