如何在aspnet核心应用程序中为静态文件使用多个目录? [英] How can you use multiple directories for static files in an aspnet core app?

查看:93
本文介绍了如何在aspnet核心应用程序中为静态文件使用多个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,wwwroot目录可以托管静态文件.但是,在某些情况下,最好为静态文件设置两个目录.(例如,让webpack将构建文件转储到一个gitignored目录中,并将一些图像文件,favicon等保存在一个非gitignored目录中).从技术上讲,这可以通过在wwwroot中具有两个文件夹来实现,但是在组织上最好在根级别上具有这些文件夹.有没有一种方法可以配置aspnet核心应用程序以将两个单独的目录用于静态文件?

By default, the wwwroot directory can host static files. However, in some scenarios, it might be ideal to have two directories for static files. (For example, having webpack dump a build into one gitignored directory, and keeping some image files, favicon and such in a not-gitignored directory). Technically, this could be achieved by having two folders within the wwwroot, but it might organizationally preferable to have these folders at the root level. Is there a way to configure an aspnet core app to use two separate directories for static files?

推荐答案

只需注册 UseStaticFiles 两次:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "static"))
});

现在可以从wwwroot和静态文件夹中找到文件.

Now files will be found from wwwroot and static folders.

这篇关于如何在aspnet核心应用程序中为静态文件使用多个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆