在 Web API 中使用 Swagger 的 AspNet Core 2.0 中的歧义

use*_*434 4 swagger asp.net-core-webapi

我正在尝试在 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, string, 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();
    }
Run Code Online (Sandbox Code Playgroud)

有没有人对此有所了解?任何帮助将不胜感激。谢谢你。

小智 5

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


Mor*_*ard 2

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