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

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

问题描述

我们有一个内部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.

包裹清单的相关部分是:

The relevant portion of the package manifest is:

<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)?

更新:我做了进一步的挖掘,发现 @Martin对类似问题的回答-但他在我回答之前就回答了有机会进行更新.
看来这种行为(将文件添加为链接)是设计使然的.

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.

但是我的问题的第二部分仍然成立.根据文档

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

默认情况下,PackageReference用于面向Windows 10 Build 15063(创建者更新)及更高版本的.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 部分控制为这些文件生成的msbuild项目,这些项目将生成 obj \ projectname.csproj.nuget.g.props 文件.

contentFiles are supposed to be added as a link. The contentFiles section controls the msbuild items that are generated for these files into the obj\projectname.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).

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

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