在Core WebApi中使用Swagger

1.打开项目NuGet包管理,搜索   Swashbuckle.AspNetCore   ,安装。

2.打开Startup.cs,添加以下代码

public void ConfigureServices(IServiceCollection services)
        {
            //swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API_1", Version = "v1" });
          //注释

           var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
           var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
           c.IncludeXmlComments(xmlPath);

            });
             //swagger
            services.AddMvc();
        }    
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //swagger
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                c.RoutePrefix = "swagger";
            });
            app.UseSwagger();
             //swagger
            app.UseMvc();

        }            

3.打开项目属性-生成,勾选以下

提醒:发布项目时,记得中生成目录将 xxx.Xml(xxx:项目名) 文件拷贝至发布根目录

posted @ 2018-12-18 09:34  Crazydragcool  阅读(180)  评论(0编辑  收藏  举报