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

1、nuget安装下面的包

 

.netcore 3.1 swagger使用_json

 

 

 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使用_笔记_02