Swagger和.Net Core 3集成

Edi*_*son 1 swagger .net-core-3.0

我正在使用swagger记录我的Web API。但是,当我运行我的项目时,它会引发错误“找不到方法:'无效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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的创业班

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();
        });

    }
Run Code Online (Sandbox Code Playgroud)

有人可以照亮,在此先感谢

bri*_*ian 9

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

  1. 在 PowerShell 中运行以下命令
Install-Package Swashbuckle.AspNetCore -Version 5.5.1
Run Code Online (Sandbox Code Playgroud)
  1. 在 Startup 类中,使用 Microsoft.OpenApi.Models 添加此引用 '''; '''
  2. 在启动类中的 ConfigureService 方法中添加以下内容:
services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    });
Run Code Online (Sandbox Code Playgroud)
  1. 下面添加到startup.cs中的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");
    })
Run Code Online (Sandbox Code Playgroud)
  1. 您可以通过 http://localhost:port/swagger 访问 swagger 页面


小智 5

我可以通过以下链接(在撰写本文时已更新为8/20/2019)来实现:

开始使用Swashbuckle和ASP.NET Core

也看看他们的GitHub链接:

AspNetCore.Docs