简体   繁体   English

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

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

I have a project written with C# on the top of ASP.NET MVC 5 framework using Visual Studio 2017. The project is running on .NET 4.7.2 framework. 我使用Visual Studio 2017在ASP.NET MVC 5框架的顶部用C#编写了一个项目。该项目在.NET 4.7.2框架上运行。

I am trying to use nuget.exe to create a package for my project so I can install it into other projects. 我正在尝试使用nuget.exe为我的项目创建一个包,以便我可以将其安装到其他项目中。

I have a folder called resources which contains .js and .css files that I don't want to include into my package. 我有一个名为resources的文件夹,其中包含我不希望包含在我的包中的.js.css文件。 I want to be able to exclude the entire resources folder. 我希望能够排除整个resources文件夹。 I also don't want to include all of my views, only views in the Views/Home folder should be included. 我也不想包含我的所有视图,只应包含Views/Home文件夹中的Views/Home

How can I exclude these files from being included in my packaged project? 如何排除这些文件包含在我的打包项目中?

I tried to follow the documentation by add exclusion on all the view then add the one I want. 我尝试通过在所有视图上添加排除来跟随文档 ,然后添加我想要的那个。 Follow is my current .nuspec file as you can see, 你可以看到Follow是我当前的.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>

UPDATED 更新

I also tried adding the same rules into the contentFiles like so 我也尝试将相同的规则添加到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>

However, ALL of the views are still being included when I install the package in a different project. 但是,当我在不同的项目中安装包时,仍然包含所有视图。

I pack my package using the following command where T:/ is my network share 我使用以下命令打包我的包,其中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? 如何将某些视图和文件夹排除在已部署的ASP.NET MVC NuGet包中?

The file element attribute exclude is correct attribute to exclude the files from the src location. 文件元素属性exclude是正确的属性,用于从src位置排除文件。 But you missing another attribute target : 但你错过了另一个属性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. 放置源文件的包中文件夹的相对路径,必须以lib,content,build或tools开头。

Check the document File element attributes for some more details. 检查文档文件元素属性以获取更多详细信息。

Otherwise, nuget package does not know where to put the files to the project when you install the package. 否则,nuget包在安装包时不知道将文件放到项目的哪个位置。

So, the .nuspec file should be like: 所以,.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>

But this package will add all the files into the the root folder of your installed project. 但是这个包会将所有文件添加到已安装项目的根文件夹中。 Because you including all the file with wildcard **\\*.* , Nuget could not to intelligently assign all of these files to the folder they should be in. 因为您包含带有通配符**\\*.*所有文件,Nuget无法智能地将所有这些文件分配到它们应该位于的文件夹中。

To resolve this issue, we should target files in different folders to different folders, like: 要解决此问题,我们应将不同文件夹中的文件定位到不同的文件夹,例如:

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

So, the .nuspec should be looks like: 所以, .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>

Then pack this .nuspec file and install it into a Console App project to test: 然后打包此.nuspec文件并将其安装到Console App项目中以进行测试:

在此输入图像描述

Hope this helps. 希望这可以帮助。

So I finally replicated your problem and figured out what it's doing. 所以我最终复制了你的问题并弄清楚它在做什么。 Basically nuget pack is looking at both your .csproj file and your .nuspec file. 基本上nuget pack正在查看.csproj文件和.nuspec文件。 It includes everything in the .csproj file marked as Build Action: Content in your project. 它包含项目中标记为Build Action: Content.csproj文件中的所有Build Action: Content Which is why your views are showing up funny and files aren't being excluded properly. 这就是为什么您的观点显示有趣并且文件未被正确排除的原因。

So to fix it you have a couple options. 所以要修复它你有几个选择。

1) Change the build action in your project for those files to not be content (just use None ) 1)更改项目中的构建操作,使这些文件不是内容(只使用None

2) Build the .nuspec file out to include all the files you need specifically like Leo's answer showed and then build with nuget pack projectname.nuspec to only pack based off of the .nuspec file ONLY 2)构建.nuspec文件以包含您需要的所有文件,例如Leo的答案显示,然后使用nuget pack projectname.nuspec构建,仅基于.nuspec文件进行.nuspec

See this answer from some other details 从其他一些细节中查看此答案

NuGet Pack: ignore default includes NuGet Pack:忽略默认包含

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM