在 ASP.NET Core MVC 项目中作为链接安装的 NuGet 包 contentFiles 工件 [英] NuGet package contentFiles artifacts installed as links in ASP.NET Core MVC project

查看:19
本文介绍了在 ASP.NET Core MVC 项目中作为链接安装的 NuGet 包 contentFiles 工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个内部 JavaScript 库,希望在多个项目之间共享.实际上,我们已经通过文件复制来共享它,但这(可以预见)导致了代码的多个分支.

We have an internal JavaScript library that we'd like to share between multiple projects. Actually we are already sharing it via file copying, but this has (predictably) resulted in multiple forks of the code.

消费项目是完整"的 ASP.NET(MVC 和 Web 窗体)和 ASP.NET Core MVC 的混合体.(我打算创建两个单独的包.)

The consuming projects are a mix of "full" ASP.NET (MVC and Web Forms) and ASP.NET Core MVC. (I'm planning on creating two separate packages.)

安装到 ASP.NET 项目中似乎可以正常工作,但我在使用 ASP.NET Core 时遇到问题.

Installing into ASP.NET projects seems to work fine, but I'm having problems with ASP.NET Core.

最初,我在 files 元素中拥有所有工件,但在使用项目中根本没有出现任何内容.重新阅读文档后,我意识到 ASP.NET Core 项目将使用 PackageReference ...所以我必须使用 contentFiles 元素而不是(或在除了)一个 files 元素.

Initially I had all the artifacts within a files element, and nothing at all was showing up in the consuming project. After re-reading the docs, I realized that ASP.NET Core projects would use a PackageReference ... so I would have to use a contentFiles element instead of (or in addition to) a files element.

我创建了一个 contentFiles 文件夹和一个脚本,将源项目文件夹结构中的必要文​​件复制到 contentFiles/any/any/wwwroot/lib/ourAwesomeWidget,并相应地修改了包清单.

I created a contentFiles folder and a script to copy the requisite files from the source project folder structure into contentFiles/any/any/wwwroot/lib/ourAwesomeWidget, and modified the package manifest accordingly.

这行得通.有点.该软件包似乎可以正确构建.这些文件确实被添加到消费项目中,但它们被添加为链接;实际文件(链接目标)驻留在我的本地包缓存中.

This works. Sort of. The package appears to get build correctly. The files do get added to the consuming project, but they get added as links; the actual files (the link targets) reside in my local package cache.

包清单的相关部分是:

<metadata minClientVersion="3.3">
    ...
    <contentFiles>
        <files include="**/*" buildAction="Content"  
               copyToOutput="true" flatten="false" />
    </contentFiles>
</metadata>
<files>
    <file src="contentFiles**" target="contentFiles" />
</files>

部分问题是我没有发现关于 contentFiles 的文档非常清楚.所有示例都显示了单个 file 元素...但是 files 元素上的 include 属性是必需的,因此不清楚个人是什么file 元素甚至可以.

Part of the issue is that I don't find the docs very clear concerning contentFiles. All the examples show a single file element ... but the include attribute on the files element is required, so it's not clear what the individual file elements would even do.

有没有办法将实际文件(不是链接)添加到消费项目中?或者,有没有办法让包安装为普通"包(而不是 PackageReference)?

Is there a way to get the actual files (not links) added to the consuming project? Or, alternatively, is there a way to get the package to install as a "normal" package (rather than a PackageReference)?

更新:我做了一些进一步的挖掘,发现 this answer by @Martin to a similar question - 但他在我之前回答了这个问题有机会更新它.
这种行为(将文件添加为链接)似乎是设计使然.

Update: I did some further digging and found this answer by @Martin to a similar question -- but he answered this one before I had a chance to update it.
It appears this behavior (adding files as links) is by design.

我觉得这非常不令人满意,因为(正如@Martin 指出的那样),我们的 JavaScript 库在消费项目的开发过程中将不可用.

I find this highly unsatisfactory, because (as @Martin points out), our JavaScript library will not be available during development on consuming projects.

但我的问题的第 2 部分仍然有效.根据 文档,

But part 2 of my question still stands. According to the docs,

默认情况下,PackageReference 用于面向 Windows 10 Build 15063(Creators Update)及更高版本的 .NET Core 项目、.NET Standard 项目和 UWP 项目.

By default, PackageReference is used for .NET Core projects, .NET Standard projects, and UWP projects targeting Windows 10 Build 15063 (Creators Update) and later.

有没有办法触发非默认行为,即允许 .NET Core 项目通过 PackageReference 以外的方式使用包?

Is there a way to trigger the non-default behavior, i.e. allow .NET Core projects to consume packages other than via PackageReference?

推荐答案

contentFiles 应该作为链接添加.contentFiles 部分控制为这些文件生成到 objprojectname.csproj.nuget.g.props 文件中的 msbuild 项.

contentFiles are supposed to be added as a link. The contentFiles section controls the msbuild items that are generated for these files into the objprojectname.csproj.nuget.g.props file.

copyToOutput="true" 将导致项目被复制到输出和发布目录.但是,在开发期间运行应用程序时,这对您没有帮助,因为它将从项目目录而不是输出目录运行.

The copyToOutput="true" will cause the items to be copied to the output and publish directory. However that does not help you when running the application during development, since it will be run from the project directory, not the output directory.

考虑 通过 npm 使用客户端库(因为不推荐使用 bower).

Consider consuming client libraries via npm (since bower is deprecated).

这篇关于在 ASP.NET Core MVC 项目中作为链接安装的 NuGet 包 contentFiles 工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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