从GitHub Package Feed安装NuGet包遇405 MethodNotAllowed错误
概述
已成功将dotnet项目模板文件打包并推送到GitHub NuGet包源,但无法拉取或安装该包。
源配置
当前使用以下命令配置源:
dotnet nuget add source https://nuget.pkg.github.com/TristanRhodes/index.json?apikey={apikey} --name TR.Packages
注:已通过用户名/密码成功认证,可绕过401错误。
Dotnet NuGet安装尝试
执行安装命令:
dotnet new install TestTemplate --nuget-source TR.Packages --force -d
得到错误:
NuGet.Protocol.Core.Types.FatalProtocolException: 无法从V2源'https://nuget.pkg.github.com/TristanRhodes/index.json/FindPackagesById()?id='TestTemplate'&semVerLevel=2.0.0'获取结果,响应状态码表示失败:405 (Method Not Allowed)。
NuGet搜索尝试
更新本地nuget后执行搜索命令:
nuget list TestTemplate
同样报错:
V2源'https://nuget.pkg.github.com/TristanRhodes/index.json/Search()?$filter=IsLatestVersion&$orderby=Id&searchTerm='TestTemplate'&targetFramework=''&includePrerelease=false&$skip=0&$top=30&semVerLevel=2.0.0'返回意外状态码'405 Method Not Allowed'。
完整安装日志
将安装以下模板包:
TestTemplate[2023-03-31 19:20:15.505] [Debug]
[Microsoft.TemplateEngine.Edge.Installers.NuGet.NuGetInstaller] =>
[Execute]: TestTemplate被识别为可下载的NuGet包。[2023-03-31 19:20:15.508] [Debug]
[Microsoft.TemplateEngine.Edge.Installers.NuGet.NuGetInstaller] =>
[Execute]: TestTemplate被识别为可下载的NuGet包。[2023-03-31 19:20:15.560] [Debug] [NuGetLogger] => [Execute]: GET https://api.nuget.org/v3/registration5-gz-semver2/testtemplate/index.json
[2023-03-31 19:20:15.584] [Debug] [NuGetLogger] => [Execute]:
正在https://nuget.pkg.github.com/TristanRhodes/index.json?apikey=%7Bapikey%7D中搜索TestTemplate。[2023-03-31 19:20:15.593] [Debug] [NuGetLogger] => [Execute]:
正在C:\Program Files (x86)\Microsoft SDKs\NuGetPackages中搜索TestTemplate。[2023-03-31 19:20:15.597] [Debug] [NuGetLogger] => [Execute]:
在NuGet源C:\Program Files (x86)\Microsoft SDKs\NuGetPackages中未找到TestTemplate。[2023-03-31 19:20:15.777] [Debug] [NuGetLogger] => [Execute]: OK https://api.nuget.org/v3/registration5-gz-semver2/testtemplate/index.json 215ms
[2023-03-31 19:20:15.834] [Debug] [NuGetLogger] => [Execute]:
在NuGet源https://api.nuget.org/v3/index.json中未找到TestTemplate。[2023-03-31 19:20:15.902] [Debug] [NuGetLogger] => [Execute]: GET https://nuget.pkg.github.com/TristanRhodes/index.json/FindPackagesById()?id=%27TestTemplate%27&semVerLevel=2.0.0
[2023-03-31 19:20:16.013] [Debug] [NuGetLogger] => [Execute]:
MethodNotAllowed https://nuget.pkg.github.com/TristanRhodes/index.json/FindPackagesById()?id=%27TestTemplate%27&semVerLevel=2.0.0 110ms[2023-03-31 19:20:16.021] [Debug] [NuGetLogger] => [Execute]: 详情:
NuGet.Protocol.Core.Types.FatalProtocolException: 无法从V2源'https://nuget.pkg.github.com/TristanRhodes/index.json/FindPackagesById()?id='TestTemplate'&semVerLevel=2.0.0'获取结果,响应状态码表示失败:405 (Method Not Allowed)。 --->
System.Net.Http.HttpRequestException: 响应状态码表示失败:405 (Method Not Allowed)。在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
在 NuGet.Protocol.HttpSource.<>c__DisplayClass15_0`1.<b__0>d.MoveNext()
--- 上一位置的堆栈跟踪结束 ---
在 NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token)
在 NuGet.Protocol.V2FeedParser.LoadXmlAsync(String uri, String cacheKey, Boolean ignoreNotFounds, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
--- 内部异常堆栈跟踪结束 ---
在 NuGet.Protocol.V2FeedParser.LoadXmlAsync(String uri, String cacheKey, Boolean ignoreNotFounds, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
在 NuGet.Protocol.V2FeedParser.QueryV2FeedAsync(String relativeUri, String id, Int32 max, Boolean ignoreNotFounds, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
在 NuGet.Protocol.V2FeedParser.FindPackagesByIdAsync(String id, Boolean includeUnlisted, Boolean includePrerelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
在 NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(String packageId, Boolean includePrerelease, Boolean includeUnlisted, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
在 Microsoft.TemplateEngine.Edge.Installers.NuGet.NuGetApiPackageManager.GetPackageMetadataAsync(PackageSource source, String packageIdentifier, Boolean includePrerelease, CancellationToken cancellationToken)。
问题
如何解决该405错误,成功获取我的NuGet包?
解决方案
1. 核心原因
GitHub Packages仅支持NuGet V3协议,而你的NuGet客户端错误地使用了V2协议接口访问,导致405 Method Not Allowed错误。此外,源地址中直接拼接apikey参数的方式可能干扰客户端对V3源的识别。
2. 修复步骤
步骤1:删除现有错误配置的源
dotnet nuget remove source TR.Packages
步骤2:重新配置GitHub NuGet源(正确方式)
使用不带apikey参数的V3源地址,并通过单独的用户名和PAT(个人访问令牌)认证:
dotnet nuget add source https://nuget.pkg.github.com/TristanRhodes/index.json --name TR.Packages --username TristanRhodes --password <你的GitHub PAT>
注意:你的GitHub PAT需要具备
read:packages权限,若之前推送包时用的是这个令牌,通常已经包含该权限。
步骤3:验证源配置
执行以下命令确认源配置正确:
dotnet nuget list source
步骤4:重新尝试安装包
dotnet new install TestTemplate --nuget-source TR.Packages --force
3. 额外排查点
- 确保本地NuGet客户端版本≥5.1(低版本可能对V3源支持不佳)
- 检查包是否在GitHub Packages中设置为公开(若为私有包,必须使用正确的PAT认证)
- 若仍有问题,可清除NuGet缓存后重试:
dotnet nuget locals all --clear
内容的提问来源于stack exchange,提问作者Tristan Rhodes

