网上的swagger教程很多,配置一堆东西,很容易跑不通,看公司同事搞得用法挺精简,适合入门使用

1、nuget安装下面的包

 

.netcore 3.1 swagger使用

 

 

 2、增加如下#region区域代码

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers();

     #region swagger
     services.AddSwaggerGen(c =>
     {
         c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "TEST API", Version = "v1" });
     });
     #endregion
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();


    #region swagger
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "TEST API V1");
    });
    #endregion


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

3、运行效果

.netcore 3.1 swagger使用

 

相关文章:

  • 2022-12-23
  • 2022-02-02
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2021-06-05
  • 2021-10-01
  • 2022-02-08
  • 2021-12-27
相关资源
相似解决方案