在NuGet软件包中包含一个文件夹,并将其作为.netcore/Razor文件安装到项目中 [英] Including a Folder in NuGet Package and have it install into project as file .netcore/Razor

查看:30
本文介绍了在NuGet软件包中包含一个文件夹,并将其作为.netcore/Razor文件安装到项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2019并使用此方法成功创建NuGet包:

这意味着如果我运行该项目时未实际复制并添加这些文件,则会得到 ArgumentException:目录名'Path-To-project \ pagesettings'不存在.(参数路径")

我知道为什么会发生这种情况,但无法解决如何更改它,或者是否有可能.

上面链接的文章建议将代码添加到 csproj 文件中,例如:

 < ItemGroup>< Content Include ="readme.txt">< Pack> true</Pack>< PackagePath> \</PackagePath></内容></ItemGroup> 

但这是行不通的,实际上似乎是不必要的,因为Pack命令包含了我的文件,只是在安装时未正确创建它们.

此外-如果有一种方法可以告诉VS提示是否安装此文件,这将非常方便.因为是设置,所以如果用户更改设置然后在以后安装我的NuGet软件包的更新版本,我不希望它覆盖其自定义设置...也许这就是链接设计发生的原因...如果是的话,如果有人可以确认吗?

解决方案

实际上,您应该在nuget包中创建一个 .props 文件.

1)在您的nuget项目中创建一个名为< package_id> .props文件的文件.

赞:

注意:如果您创建的名为 test.1.0.0.nupkg 的nuget包,则文件应命名为 test.props ,因此它会起作用.

2)将它们添加到 test.props 文件中:

 < Project><目标名称="CopyFiles"BeforeTargets ="Build"< ItemGroup><文件包含=" $(MSBuildThisFileDirectory).. \ Pagesettings \ *.*></File></ItemGroup>< Copy SourceFiles ="@(文件)";DestinationFolder ="$(ProjectDir)Pagesettings"</Copy></Target></Project> 

3)将它们添加到 xxx.csproj 文件中:

 < ItemGroup><无包含="Pagesettings \ *.*(您的nuget项目的json文件的路径)"Pack ="true"PackagePath ="Pagesettings"</无><无包含="build \ *.*"Pack ="true"PackagePath ="build"</None></ItemGroup> 

然后重新获得您的项目.

4),然后与此类似的问题.

I am using Visual Studio 2019 and creating NuGet packages successfully with this method:

https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli

All going well, but there are some settings (.json) files contained within a directory PageSettings/

When I publish my NuGet package and then install it into a new project, this directory appears in VS as a linked item (see pic). So as a user I can access the files, but they don't "exist" when the project is run.

This means if I run this project without physically copying and adding these files I get ArgumentException: The directory name 'Path-To-project\pagesettings' does not exist. (Parameter 'Path')

I can see why this is happening, but can't work out how to change it, or if it is possible.

The article linked above suggests adding code to the csproj file like:

<ItemGroup>
  <Content Include="readme.txt">
    <Pack>true</Pack>
    <PackagePath>\</PackagePath>
  </Content>
</ItemGroup>

But that doesn't work and in fact seems unnecessary since the Pack command is including my files, just not creating them properly when installing.

Also - it would be extremely handy if there was a way to tell VS to prompt whether to install this file or not. Since it is settings, if a user changes the settings and then later installs an updated version of my NuGet package I would not want it to overwrite their customised settings... perhaps this is why the link design happens... if so, if someone could confirm?

解决方案

Actually, you should create a .props file in your nuget package.

1) create a file called <package_id>.props file in your nuget project.

Like this:

Note: if your created nuget package called test.1.0.0.nupkg, the file should be named as test.props so that it will work.

2) add these in the test.props file:

<Project>
  <Target Name="CopyFiles" BeforeTargets="Build">
    <ItemGroup>     
      <File Include="$(MSBuildThisFileDirectory)..\Pagesettings\*.*"></File>          
    </ItemGroup>
      <Copy SourceFiles="@(File)" DestinationFolder="$(ProjectDir)Pagesettings"></Copy>
  </Target>
</Project>

3) add these in xxx.csproj file:

   <ItemGroup>
        <None Include="Pagesettings\*.*(the json files' path of your nuget project)" Pack="true" PackagePath="Pagesettings"> 
        </None>
        <None Include="build\*.*" Pack="true" PackagePath="build"></None>
    </ItemGroup>

then reapck your project.

4) then clean your nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.

5) when you insall this new version of the nuget package, please build your main project again to run the target to generate the files.

Besides, there is also a similar issue about this.

这篇关于在NuGet软件包中包含一个文件夹,并将其作为.netcore/Razor文件安装到项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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