.net core Swagger

1.环境.net core 2.0

Startup中的  ConfigureServices

//注册 Swagger
            services.AddSwaggerGen(sg =>
            {
                sg.SwaggerDoc("refuge", new Swashbuckle.AspNetCore.Swagger.Info
                {
                    Title = "我的第一个 Swagger",
                    Version = "版本1"
                });
            });

Configure 启用中间件

//启用 Swagger
            app.UseSwagger();
            app.UseSwaggerUI(s =>
            {
                s.SwaggerEndpoint("/swagger/refuge/swagger.json", "My API 1.0.1");//注意,中间那段的名字 (refuge) 要和 上面 SwaggerDoc 方法定义的 名字 (refuge)一样
                s.RoutePrefix = string.Empty; //默认值是 "swagger" ,需要这样请求:https://localhost:44384/swagger
            });

 

 

 

2.环境.net core 3.1

安装nuget包:

Swashbuckle.AspNetCore

 

 Startup:

 public void ConfigureServices(IServiceCollection services)
 {// 添加Swagger
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Demo", Version = "v1" });
    });

     services.AddControllers();
 }
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 {
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }

     app.UseHttpsRedirection();

     app.UseRouting();// 添加Swagger有关中间件
     app.UseSwagger();
     app.UseSwaggerUI(c =>
     {
         c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Demo v1");
     });

     app.UseAuthorization();

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

 

访问:http://localhost:50383/swagger/index.html

 

posted @ 2019-04-03 17:12  阳光下的行者  阅读(232)  评论(0编辑  收藏  举报