如何排除某些视图和文件夹不包含在已部署的 ASP.NET MVC NuGet 包中?

How can I exclude some views and folders from being included into the deployed ASP.NET MVC NuGet package?

我有一个使用 Visual Studio 2017 在 ASP.NET MVC 5 框架之上用 C# 编写的项目。该项目是 运行 on .NET 4.7.2框架。

我正在尝试使用 nuget.exe 为我的项目创建一个包,以便我可以将它安装到其他项目中。

我有一个名为 resources 的文件夹,其中包含我不想包含在我的包中的 .js.css 文件。我希望能够排除整个 resources 文件夹。我也不想包含我的所有视图,只应包含 Views/Home 文件夹中的视图。

如何排除这些文件不包含在我的打包项目中?

我尝试通过在所有视图上添加排除项来遵循 documentation,然后添加我想要的视图。如您所见,下面是我当前的 .nuspec 文件,

<?xml version="1.0"?>
<package >
  <metadata>
    <id>....</id>
    <version>1.0.0</version>
    <title>...</title>
    <authors>...</authors>
    <owners>...</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>...</description>
    <releaseNotes></releaseNotes>
    <copyright>2019</copyright>
    <tags></tags>
  </metadata>  

   <files>

       <file src="**\*.*" exclude="**\*.designer.cs;**\*.csproj;**\*.pdb;**\*.user;**\*.vspscc;**\*.nuspec;libman.json;package.json;packages.config;Web.config;ApplicationInsights.config;public\**\*.*;Resource\**\*.*;bin\**\*.*;node_modules\**\*.*;obj\**\*.*;**\*.log;Views\**\*.*"/>
       <file src="Views\Home\**\*.cshtml" />

   </files>
</package>

已更新

我也尝试将相同的规则添加到 contentFiles 中,就像这样

<?xml version="1.0"?>
<package >
  <metadata>
    <id>....</id>
    <version>1.0.0</version>
    <title>...</title>
    <authors>...</authors>
    <owners>...</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>...</description>
    <releaseNotes></releaseNotes>
    <copyright>2019</copyright>
    <tags></tags>
    <contentFiles>
       <files include="**\*.*" exclude="**\*.designer.cs;**\*.csproj;**\*.pdb;**\*.user;**\*.vspscc;**\*.nuspec;libman.json;package.json;packages.config;Web.config;ApplicationInsights.config;public\**\*.*;Resource\**\*.*;bin\**\*.*;node_modules\**\*.*;obj\**\*.*;**\*.log;Views\**\*.*"/>
       <file include="Views\Home\**\*.cshtml" />
    </contentFiles>
  </metadata>  

   <files>

       <file src="**\*.*" exclude="**\*.designer.cs;**\*.csproj;**\*.pdb;**\*.user;**\*.vspscc;**\*.nuspec;libman.json;package.json;packages.config;Web.config;ApplicationInsights.config;public\**\*.*;Resource\**\*.*;bin\**\*.*;node_modules\**\*.*;obj\**\*.*;**\*.log;Views\**\*.*"/>
       <file src="Views\Home\**\*.cshtml" />

   </files>
</package>

然而,当我在不同的项目中安装包时,所有的视图仍然被包括在内。

我使用以下命令打包我的包,其中 T:/ 是我的网络共享

nuget pack -Prop Configuration=Release -OutputDirectory T:/

How can I exclude some views and folders from being included into the deployed ASP.NET MVC NuGet package?

文件元素属性 exclude 是从 src 位置排除文件的正确属性。但是您缺少另一个属性 target:

The relative path to the folder within the package where the source files are placed, which must begin with lib, content, build, or tools.

查看文档 File element attributes 了解更多详细信息。

否则,安装包时,nuget包不知道把文件放到项目的什么位置。

所以,.nuspec 文件应该是这样的:

   <files>

       <file src="**\*.*" exclude="**\*.designer.cs;**\*.csproj;**\*.pdb;**\*.user;**\*.vspscc;**\*.nuspec;libman.json;package.json;packages.config;Web.config;ApplicationInsights.config;public\**\*.*;Resource\**\*.*;bin\**\*.*;node_modules\**\*.*;obj\**\*.*;**\*.log;Views\**\*.*" target="content\/>
       <file src="Views\Home\**\*.cshtml" target="content\Views\Home\" />

   </files>

但是这个包会将所有文件添加到您安装的项目的根文件夹中。因为您使用通配符 **\*.* 包含了所有文件,所以 Nuget 无法智能地将所有这些文件分配到它们应该位于的文件夹中。

要解决这个问题,我们应该将不同文件夹中的文件定位到不同的文件夹,例如:

<file src="App_Start\*.cs" target="content\App_Start\" />

所以,.nuspec 应该是这样的:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>TestWebApp</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2019</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>

  <files>
    <file src="App_Start\*.cs" target="content\App_Start\" />
    <file src="resources\*.*" target="content\resources\" />
     ....
    <file src="Views\Home\**\*.cshtml" target="content\Views\Home\" />
  </files>

</package>

然后将这个.nuspec文件打包安装到Console App项目中进行测试:

希望这对您有所帮助。

所以我终于复制了你的问题并弄清楚了它在做什么。基本上 nuget pack 正在查看您的 .csproj 文件和 .nuspec 文件。它包括项目中标记为 Build Action: Content.csproj 文件中的所有内容。这就是为什么您的视图看起来很有趣并且文件没有被正确排除的原因。

所以要修复它,您有几个选择。

1) 将项目中的构建操作更改为不包含内容(只需使用 None

2) 构建 .nuspec 文件以包含您需要的所有文件,如 Leo 的回答所示,然后使用 nuget pack projectname.nuspec 构建以仅基于 .nuspec 文件打包只有

从其他一些细节看这个答案