如何将Resx文件包含在Nuget软件包中

问题描述

我有一个.net Framework 4.6.1项目,我从中制作了一个nuget程序包。这个nuget软件包仅安装了2个dll和几个内容文件效果很好。

问题是我现在通过驻留在以下位置的resx文件添加了资源字符串:

~\App_Data\Global\Resources\resource-strings.resx

我不知道如何使该文件正确地成为nuget包的一部分。创建.nupkg时,我在那看到了resx文件,但是当我将其安装在另一个项目中时,该resx文件应该复制到App_Data \ Global \ Resources文件夹中,但是没有。

有可能吗?

根据我的调查,我认为我还必须对目标文件+ nuspec配置进行一些操作,但是我没有尝试过。

我实际上只是需要将resx文件复制过来。没有什么比这更复杂了。

解决方法

确定。这是可能的,可以通过nuget完成。由于您希望将此资源文件复制到目标asp net项目文件夹中,因此可以尝试以下步骤:

=========================================== ======

首先,如果要将此net461 nuget软件包安装到net framework asp net项目中,则应在xxx.nusepc中使用content node文件

首先,请确保resource-strings.resx的 Build Action Embedded Resource ,而不是 Content

1) 首先,运行cmd命令:cd xxxx(project folder path),然后运行nuget spec以生成nuspec文件。这些就足够了:

2)打开nuspec文件并添加内容节点:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>xxx</id>
        <version>xxx</version>
        <title>xxx</title>
        <authors>xxx</authors>
        ............       
    </metadata>
    <files>
        <file src="~\App_Data\Global\Resources\resource-strings.resx(the path of the file in net framework 4.6.1 project)" target="content\App_Data\Global\Resources\resource-strings.resx" />
    </files>
</package>

3),然后保存nuspec文件并运行nuget pack来生成nupkg

之前,安装nuget软件包之前,应先clean nuget caches删除旧的错误版本的nuget。

安装此软件包时,该文件将被复制到Web项目的根路径App_Data\Global\Resources\resource-strings.resx中。

================================================ =====

如果要将此程序包安装到新的sdk项目(Net Core或xxx,具有 PackageReference nuget管理格式),则应使用复制任务创建目标文件。

1)在net framework 4.6.1项目中添加一个名为build的文件夹,然后添加一个名为<Package_id>.props的文件。

请注意,请确保nuget软件包的ID与<Package_id>.props相同。 Hint from here

2)将它们添加到<Package_id>.props

  <Project>
      <Target Name="CopyFilesToProject" BeforeTargets="Build">
        <Message Text="Copy resource-strings.resx  to project" />
        <ItemGroup>
          <SourceScripts Include="$(MSBuildThisFileDirectory)..\content\**\*.*"/>
        </ItemGroup>
        <Copy
           SourceFiles="@(SourceScripts)"
           DestinationFiles="$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)"/>
      </Target>
      
    </Project>

3)像这样修改xxx.nuspec文件:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>xxx</id>
    <version>xxx</version>
    <title>xxx</title>
    <authors>xxx</authors>
    <owners>me</owners>
    ............
  </metadata>
 <files>
<file src="~\App_Data\Global\Resources\resource-strings.resx" target="content\App_Data\Global\Resources\resource-strings.resx" />
<file src="build\xxx(like package_id).props" target="build"/>
</files>

</package>

4),则应使用nuget pack命令打包该项目。在安装此软件包之前,您应该先清理nuget缓存。

安装此nuget软件包后,应构建项目以运行此自定义复制目标,以将文件复制到主项目中。

,与此有关的a similar issue