我有一个ASP。NET MVC5应用程序,昨天工作,现在我得到这个错误时,我试图构建:

该项目引用了这台计算机上缺少的NuGet包。

我已经检查了两个选项,允许nuget自动下载和安装丢失的包检查/打开。我还尝试删除packages文件夹中的所有文件,然后让nuget重新下载它们。另外,当我打开nuget并寻找更新时,它说没有需要安装的。我想不出还能做些什么来解决这个恼人的问题。

我还通过右键单击项目并选择该选项启用了nuget恢复。然后,它添加了一个nuget文件夹和该文件夹中的三个项目,但没有采取任何措施来解决问题。我已经尝试重新构建,但仍然得到相同的错误。


当前回答

以我为例,我把我的解决方案文件夹从一个位置移动到另一个位置,重新组织了一下,在这个过程中,它的相对文件夹结构发生了变化。

因此,我必须编辑我的.csproj文件中与下面类似的所有条目

  <Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

to

  <Import Project="packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

(注意从..\packages\到packages\。在你的情况下,它可能是一个不同的相对结构,但你懂的。)

其他回答

如果您正在使用TFS

删除NuGet.exe和NuGet。目标解决方案的.nuget文件夹中的文件。确保文件本身也从解决方案工作区中删除。 保留NuGet。配置文件继续绕过向源代码控制添加包。

编辑解决方案中的每个项目文件(例如,.csproj, .vbproj),并删除对NuGet的任何引用。目标文件。在您选择的编辑器中打开项目文件并删除以下设置:

<RestorePackages>true</RestorePackages>  
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
    <PropertyGroup>
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

如果您不使用TFS

从解决方案中删除.nuget文件夹。确保文件夹本身也从解决方案工作区中删除。

编辑解决方案中的每个项目文件(例如,.csproj, .vbproj),并删除对NuGet的任何引用。目标文件。在您选择的编辑器中打开项目文件并删除以下设置:

<RestorePackages>true</RestorePackages>  
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
    <PropertyGroup>
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

参考:迁移msbuild集成解决方案以使用自动包恢复

以下是我用来解决这个问题的步骤:

向解决方案中添加nuget包:

右键单击要引用nuget的项目(而不是解决方案) 包。 选择:管理nuget包 在弹出窗口中,左边有三个选择。 如果你选择在线>微软和。net,你将能够安装 微软的ASP。NET Web API 2.2包grouper(或任何包 你需要——我的是这个)。 现在右键单击您的解决方案(不是项目)并进行选择 启用nuget包恢复。这将导致包在编译时自动下载。

经过一段时间解决这个问题。 只需添加一个包源https://api.nuget.org/v3/index.json解决我这边的错误

我有同样的问题,当我引用类库到我的MVC web应用程序,

问题是两个项目之间的nuget包版本号不匹配。

例如:我的类库有1.2.3的log4net,但我的webapp有1.2.6

修正:只要确保两个项目有相同的版本号引用。

在我的例子中,它与Microsoft.Build.Bcl版本有关。 我的nuget包版本是1.0.21,但我的项目文件仍然指向1.0.14版本

所以我把我的。csproj文件从:

  <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
   <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
  </Target>

to:

 <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
  <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />

这个结构又开始工作了。