Swagger and .Net Core 3 integration

一曲冷凌霜 提交于 2020-07-09 08:56:12

问题


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 youtube. 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


回答1:


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




回答2:


Here is the least amount of step in order to add the swagger to your project.

  1. Run below command at PowerShell
Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4
  1. In the Startup class, add this reference ''' using Microsoft.OpenApi.Models; '''
  2. Add below to ConfigureService method in the startup class:
services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    });
  1. Add below to the Configure method in the startup.cs
// 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. You can access the swagger page at http://localhost:port/swagger



回答3:


I had this issue and all I needed to do is to update Nuget Package Swashbuckle.AspNetCore to version 5.4.1 or install it if you don't have it.



来源:https://stackoverflow.com/questions/58142734/swagger-and-net-core-3-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!