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

ASP.NET Core中AddVersionedApiExplorer报CS1061错误求助

解决ASP.NET Core中AddVersionedApiExplorer的CS1061编译器错误

常见修复方案

  • 修正服务注册的链式调用逻辑
    CS1061错误大多是因为扩展方法未被正确识别,Asp.Versioning新版本已将API Explorer的注册整合到AddApiVersioning的链式调用中,替换旧的AddVersionedApiExplorer为AddApiExplorer即可:

    builder.Services.AddApiVersioning(options =>
    {
        options.ReportApiVersions = true;
        options.AssumeDefaultVersionWhenUnspecified = true;
        options.DefaultApiVersion = new ApiVersion(1, 0);
    }).AddApiExplorer(options =>
    {
        options.GroupNameFormat = "'v'VVV";
        options.SubstituteApiVersionInUrl = true;
    });
    
  • 确保包版本完全一致
    Asp.Versioning.Mvc和Asp.Versioning.Mvc.ApiExplorer版本不匹配会导致扩展方法无法被编译器识别,检查项目文件(.csproj)中的包引用,保证版本号相同:

    <PackageReference Include="Asp.Versioning.Mvc" Version="7.0.0" />
    <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />
    
  • 清理编译器缓存并重建项目
    缓存残留可能引发错误,执行以下操作:

    • 删除项目目录下的bin和obj文件夹
    • 执行命令:dotnet clean → dotnet restore → dotnet build
    • 重启开发工具(如Visual Studio)
  • 验证命名空间引用有效性
    右键点击代码中的AddApiExplorer,选择“转到定义”,确认能定位到Asp.Versioning.ApiExplorer命名空间下的扩展方法。若无法定位,检查命名空间拼写是否正确,或重新安装对应包。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 20:54:59