Swagger 和 .Net Core 3 集成

Swagger and .Net Core 3 integration

我正在使用 swagger 记录我的网络 api。但是,当我 运行 我的项目时,它会抛出错误 "Method not found: 'Void Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware..."。

我只跟着youtube上的教程。不确定错误的原因是什么。

我的项目是.Net Core 3。

 internal class SwaggerConfiguration
{
    public static void Configure(IServiceCollection services)
    {
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info() { Title = "Sample Portal API", Version = "1.0.0.0" });
        });
    }

    public static void Configure(IConfiguration configuration, IApplicationBuilder app)
    {
        var swaggerOptions = new SwaggerOptions(configuration).Bind();

        app.UseSwagger(options =>
        {
            options.RouteTemplate = swaggerOptions.Route;
        });


        app.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint(swaggerOptions.UIEndpoint, swaggerOptions.Description);
        });
    }
    private class SwaggerOptions
    {
        IConfiguration _configuration;
        public SwaggerOptions(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public string Route { get; set; }
        public string Description { get; set; }
        public string UIEndpoint { get; set; }

        public SwaggerOptions Bind()
        {
            _configuration.GetSection(nameof(SwaggerOptions)).Bind(this);
            return this;
        }
    }
}

这是我的创业公司class

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

        SwaggerConfiguration.Configure(services);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        SwaggerConfiguration.Configure(Configuration, app);

        app.UseHttpsRedirection();

        app.UseRouting();
        app.UseAuthorization();


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }

谁能给个提示,先谢谢了

我能够通过遵循此 link(截至撰写本文时已于 2019 年 8 月 20 日更新):

Get started with Swashbuckle and ASP.NET Core

也看看他们的 GitHub link:

AspNetCore.Docs

这是为您的项目添加 swagger 的最少步骤。

  1. 运行 在 PowerShell 下的命令
Install-Package Swashbuckle.AspNetCore -Version 5.5.1
  1. 在启动 class 中,添加此引用 ''' 使用 Microsoft.OpenApi.Models; '''
  2. 在启动的ConfigureService方法中添加如下内容class:
services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    });
  1. 在 startup.cs
  2. 中的 Configure 方法中添加以下内容
// Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    })
  1. 您可以在 http://localhost:port/swagger
  2. 访问 swagger 页面

我遇到了这个问题,我需要做的就是将 Nuget 包 Swashbuckle.AspNetCore 更新到版本 5.4.1 或如果没有安装它。