如何修复 Visual Studio 2022 中包源映射关闭的问题?

754 阅读1分钟

在网上下载了一个项目,点击还原包,结果出现包源映射关闭。于是带着这个关键词语在网上搜索。一会儿就找到了。于是做了一个小小的笔记。

参考连接:www.soinside.com/question/EZ…

learn.microsoft.com/en-us/nuget…

我是复制了这个文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- Define the package sources, nuget.org and contoso.com. -->
  <!-- `clear` ensures no additional sources are inherited from another config file. -->
  <packageSources>
    <clear />
    <!-- `key` can be any identifier for your source. -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="contoso.com" value="https://contoso.com/packages/" />
  </packageSources>
  
  <!-- Define mappings by adding package patterns beneath the target source. -->
  <!-- Contoso.* packages and NuGet.Common will be restored from contoso.com,
       everything else from nuget.org. -->
  <packageSourceMapping>
    <!-- key value for <packageSource> should match key values from <packageSources> element -->
    <packageSource key="nuget.org">
      <package pattern="*" />
    </packageSource>
    <packageSource key="contoso.com">
      <package pattern="Contoso.*" />
      <package pattern="NuGet.Common" />
    </packageSource>
  </packageSourceMapping>
</configuration>

然后在C盘搜索NuGet.Config 这个文件。找到打开,复制上面的内容。再参考这个目录,把包还原路径也加了。 www.cnblogs.com/Andy-Blog/p…

于是我的文件是

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- Define the package sources, nuget.org and contoso.com. -->
  <!-- `clear` ensures no additional sources are inherited from another config file. -->
  <packageSources>
    <clear />
    <!-- `key` can be any identifier for your source. -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="contoso.com" value="https://contoso.com/packages/" />
  </packageSources>
  
  <!-- Define mappings by adding package patterns beneath the target source. -->
  <!-- Contoso.* packages and NuGet.Common will be restored from contoso.com,
       everything else from nuget.org. -->
  <packageSourceMapping>
    <!-- key value for <packageSource> should match key values from <packageSources> element -->
    <packageSource key="nuget.org">
      <package pattern="*" />
    </packageSource>
    <packageSource key="contoso.com">
      <package pattern="Contoso.*" />
      <package pattern="NuGet.Common" />
    </packageSource>
  </packageSourceMapping>
  <config> 
    <add key="globalPackagesFolder" value="D:\nuget\packages" />
</config>
</configuration>

再次回到vs 发现已经找到包源映射

image.png

接着点击还原nuget 包

image.png

不一会儿 D盘 。D:\nuget\packages 目录下载了很多文件

image.png

还原成功,点击运行项目。