ASP.NET Core with Angular 5 -- 何时使用资产文件夹与 wwwroot 文件夹?

ASP.NET Core with Angular 5 -- When to use the assets folder vs. the wwwroot folder?

将 Angular 5 与 ASP.NET 核心一起使用时,新的 Visual Studio 项目包含 wwwroot 文件夹和资产文件夹:

问题是:静态内容(图片、css等)应该放在哪个文件夹中,什么时候用一个和另一个?

根据answers网上的说法,wwwroot文件夹是静态内容应该去的地方:

The wwwroot folder is new in ASP.NET 5.0. All of the static files in your project go into this folder. These are assets that the app will serve directly to clients, including HTML files, CSS files, image files, and JavaScript files.

然而,根据Angular's documentation,静态内容也可以进入资产文件夹:

You use the assets array in .angular-cli.json to list files or folders you want to copy as-is when building your project.

Angular 中使用和引用的所有静态内容都应归入资产。 ASP.NET 核心中 MVC 视图中使用的所有静态内容应直接转到 wwwroot。

在 build/deployment/publish 期间,构建的 Angular 应用程序将自动复制到 wwwroot,因为这是 ASP.NET 核心应用程序静态内容的根文件夹。 (因此 Angular 工件从 ASP.NET 核心视图变为静态内容。但您不需要手动执行此操作。)

只是一些关于此的附加信息。今天我 运行 在查看我公司即将重新设计的应用程序时遇到了类似的情况,我在 wwwroot 和资产文件夹中发现了静态文件。

根据问题中的屏幕截图,项目已使用 Visual Studio 附带的 Net Core Angular 模板创建:https://docs.microsoft.com/en-us/aspnet/core/client-side/spa/angular?view=aspnetcore-3.1&tabs=visual-studio(我在项目中使用的相同模板今天正在看)。默认情况下,你会得到一个像这样的文件夹结构(附上新项目的截图):

其中 favicon.ico 位于 wwwroot 文件夹中,因此您可以继续在 wwwroot 文件夹中添加图像和其他静态文件,应用程序将正常运行。该模板配置为在生产中使用文件夹“ClientApp/dist”(图片来自使用 .NET Core 3.1 的 Angular 模板创建的新项目)假设您将所有服务作为一项服务提供。

因此,这两种方法都可以,但资产文件夹似乎是一个更好的地方。那是我作为开发人员期望为 Angular 应用程序找到静态文件的地方。

我看到将静态文件保存在 Angular 项目(文件夹 ClientApp/src/assets)中的实际优势是,如果您决定单独部署和服务客户​​端应用程序,您可以获取包含资产文件夹的 angular 构建并将它们移动到服务器,无需额外工作。