.NET Core 3.0 WebApi 使用Swagger

1、安装指定版本: Swashbuckle.AspNetCore 5.0.0-rc4(目前稳定版本4.0.1在AspNetCore3.0中会报错误)

 

 

2、后台C#代码要严格格式必须加[HttpPost]或者[HttpGet]

3、Startup类中配置如下代码

     


public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddRazorPages(); #region Swagger配置 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" }); c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); });

#endregion
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
}





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


      app.UseRouting();
      app.UseStaticFiles();//启动静态文件
      app.UseAuthorization();


      #region session配置
       app.UseSession();
      #endregion


      #region Swagger配置
       //启用中间件服务生成Swagger作为JSON终结点
       app.UseSwagger();
      //启用中间件服务对swagger-ui,指定Swagger JSON终结点
      app.UseSwaggerUI(c =>
      {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
      });
     #endregion


     app.UseEndpoints(endpoints =>
     {
      endpoints.MapControllers();
      endpoints.MapAreaControllerRoute(
      name: "areas", "Admin",
      pattern: "{area:exists}/{controller=Users}/{action=Index}/{id?}");
      endpoints.MapRazorPages();
     });

  }




  

 

 

4、地址栏输入地址(注意端口每个人的机器上的端口不一样):http://localhost:56534/swagger/v1/swagger.json

 

 

 

 

5、地址栏输入地址:http://localhost:56534/swagger/index.html

posted @ 2019-11-14 14:48  斌言  阅读(603)  评论(0编辑  收藏  举报