且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swagger 和 .Net Core 3 集成

更新时间:2023-02-14 17:45:25

我可以通过点击此链接(在撰写本文时于 2019 年 8 月 20 日更新)::>

开始使用 Swashbuckle 和 ASP.NET Core

也看看他们的 GitHub 链接:

AspNetCore.Docs

I'm documenting my web api using swagger. However when I run my Project, it throws an error "Method not found: 'Void Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware...".

I only followed the tutorials from ***. Not sure what is the cause of error.

My project is .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;
        }
    }
}

this is my startup 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();
        });

    }

Can someone shed a light, thanks in advance

I was able to get it going by following this link (updated 8/20/2019 as of this writing):

Get started with Swashbuckle and ASP.NET Core

Take a look at their GitHub link too:

AspNetCore.Docs