【问题标题】:Swagger and .Net Core 3 integrationSwagger 和 .Net Core 3 集成
【发布时间】:2019-09-28 01:40:57
【问题描述】:

我正在使用 swagger 记录我的 web api。但是,当我运行我的项目时,它会引发错误“找不到方法:'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;
        }
    }
}

这是我的启动课

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

    }

有人能指点一下吗,先谢谢了

【问题讨论】:

    标签: swagger .net-core-3.0


    【解决方案1】:

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

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

    【讨论】:

    • 比接受的只是一个链接的答案更好
    【解决方案2】:

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

    Get started with Swashbuckle and ASP.NET Core

    也看看他们的 GitHub 链接:

    AspNetCore.Docs

    【讨论】:

    • 非常感谢!有用!所以.Net Core 3 的大摇大摆仍在预构建中。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      • 2020-02-12
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多