You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过NuGet API查询包是否unlisted/deprecated及获取相关原因参考?

Great question! The NuGet API absolutely has support for checking if a package version is unlisted or deprecated, and in the case of deprecated packages, you can pull in additional details like the reason for deprecation or recommended alternatives. Here's how it works:

NuGet API for Package Status Checks

1. Checking if a Package Version is Unlisted

To check if a specific package version is unlisted, you can use the NuGet Registration API (V3 endpoint). The key field to look for is listed — a boolean value where false means the version is unlisted.

For example, to fetch metadata for all versions of a package (like Newtonsoft.Json), send a GET request to:

GET https://api.nuget.org/v3/registration5-semver1/newtonsoft.json/index.json

Each version entry in the response will include the listed property. If you only care about a single version, you can target the specific version's endpoint directly:

GET https://api.nuget.org/v3/registration5-semver1/newtonsoft.json/12.0.1.json

Note: Unlisted versions don't have a standardized field for a "reason" — this is because unlisting is typically a publisher's choice to hide a version without formal deprecation, so NuGet doesn't enforce capturing a reason for it.

2. Checking if a Package Version is Deprecated

Deprecated packages have dedicated metadata in the API response, stored in the deprecation object. This object includes all the details you're looking for:

Deprecation Details Available:

  • reason: A predefined enum value (e.g., Legacy, CriticalBugs, Other) that categorizes why the package was deprecated
  • message: A custom text message from the publisher explaining the deprecation in more detail
  • alternatePackage: An object pointing to a recommended replacement package, including its ID and version range

Example Deprecation Response Snippet

Here's what a deprecated version's metadata might look like (formatted for readability):

{
"version": "12.0.1",
"listed": true,
"deprecation": {
"reason": "CriticalBugs",
"message": "This version contains a critical serialization bug that affects nested objects. Please upgrade to v13.0.0 or later.",
"alternatePackage": {
"id": "Newtonsoft.Json",
"range": "[13.0.0,)"
}
}
}

3. Using NuGet.Client for Easier Access

If you're working in a .NET project, you can use the official NuGet.Client library to avoid manually handling HTTP requests. This library provides strongly-typed resources to fetch package metadata:

  • Use PackageMetadataResource to retrieve version metadata
  • Access the PackageVersionMetadata.Listed property to check unlisted status
  • Access the PackageVersionMetadata.Deprecation property to get deprecation details (if present)

For example, a quick snippet using the library:

var packageSource = new PackageSource("https://api.nuget.org/v3/index.json");
var sourceRepository = Repository.Factory.GetCoreV3(packageSource);
var metadataResource = await sourceRepository.GetResourceAsync<PackageMetadataResource>();

var metadata = await metadataResource.GetMetadataAsync(
    "Newtonsoft.Json",
    true,
    false,
    new SourceCacheContext(),
    NullLogger.Instance,
    CancellationToken.None);

foreach (var versionMetadata in metadata)
{
    Console.WriteLine($"Version: {versionMetadata.Version}");
    Console.WriteLine($"Unlisted: {!versionMetadata.Listed}");
    if (versionMetadata.Deprecation != null)
    {
        Console.WriteLine($"Deprecated Reason: {versionMetadata.Deprecation.Reason}");
        Console.WriteLine($"Deprecation Message: {versionMetadata.Deprecation.Message}");
        if (versionMetadata.Deprecation.AlternatePackage != null)
        {
            Console.WriteLine($"Recommended Alternative: {versionMetadata.Deprecation.AlternatePackage.Id} ({versionMetadata.Deprecation.AlternatePackage.Range})");
        }
    }
}

内容的提问来源于stack exchange,提问作者rychlmoj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:37:29