【问题标题】:Ambiguites in AspNet Core 2.0 using Swagger in Web API在 Web API 中使用 Swagger 的 Asp Net Core 2.0 中的歧义
【发布时间】:2018-04-16 12:04:58
【问题描述】:

我正在尝试在 AspNet Core 2 Web API 中使用 Swagger。我有一个样本基于:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

但是,当我尝试在另一个服务中使用相同的方法时,我得到了编译错误:

2>Startup.cs(41,17,41,27):错误 CS0121:

以下方法或属性之间的调用不明确: 'Microsoft.AspNetCore.Builder.SwaggerBuilderExtensions.UseSwagger(Microsoft.AspNetCore.Builder.IApplicationBuilder, System.Action)'

'Microsoft.AspNetCore.Builder.SwaggerBuilderExtensions.UseSwagger(Microsoft.AspNetCore.Builder.IApplicationBuilder, 字符串,System.Action)'

2>完成构建项目“SocialNetwork.Api.csproj”——失败。

目标调用位于 Startup.cs 的 Configure 方法中。

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger(); // Ambiguous

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "SocialNetwork API V1");
        });

        app.UseMvc();
    }

有人对此有见解吗?任何帮助将不胜感激。 谢谢。

【问题讨论】:

  • 似乎你陷入了扩展方法歧义引用问题。尝试删除 usings 并显式调用它们。例如,您可以使用MvcApplicationBuilderExtensions.UseMvc(app); 而不是app.UseMvc();
  • 我有这个问题,试图删除 NSwag,但似乎无法摆脱 NSwag。如果我删除 NSwag 并安装了 swashbuckle,我最终会得到:CS0121 以下方法或属性之间的调用不明确:Microsoft.AspNetCore.Builder.NSwagApplicationBuilderExtensions.UseSwagger(Microsoft.AspNetCore.Builder.IApplicationBuilder, System.Action) 和 Microsoft.AspNetCore.Builder.SwaggerBuilderExtensions.UseSwagger(Microsoft.AspNetCore.Builder.IApplicationBuilder, System.Action)

标签: swagger asp.net-core-webapi


【解决方案1】:

我卸载了 Swagger 并刚刚安装了 SwashBuckle.AspNetCore,因为它已经在内部实现了 Swagger 对象。这就是为什么它被称为模棱两可。

【讨论】:

    【解决方案2】:

    可能是您引用了多个 Swagger 库吗?就我而言,我不小心添加了对其他与 swagger 相关的 Nuget 包的引用,这导致了与您描述的相同的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-16
      • 2018-05-13
      • 1970-01-01
      • 2019-01-24
      • 2019-06-29
      • 2021-05-15
      • 2020-03-23
      • 2018-03-26
      相关资源
      最近更新 更多