.net core web api 使用swagger
.net 的就不用说了,看这位老哥的 http://www.cnblogs.com/alunchen/p/6888002.html
现在来讲.net core
Nuget Packages安装,使用程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore -Pre
Startup 配置
public void ConfigureServices(IServiceCollection services) { services.AddMvc() .AddJsonOptions(options=>options.SerializerSettings.ContractResolver =new Newtonsoft.Json.Serialization.DefaultContractResolver());//json首字母小写 services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Version = "v1", Title = "TestSwagger API" }); //Determine base path for the application. var basePath = PlatformServices.Default.Application.ApplicationBasePath; //Set the comments path for the swagger json and ui. var xmlPath = Path.Combine(basePath, "TestSwagger.xml"); options.IncludeXmlComments(xmlPath); }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseMvc(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestSwagger API V1"); }); }
另外上图中,禁止显示警告中,添加1591 代码,可以过滤掉一些类名没有写注释的报警信息。
然后,就可以运行啦
每次启动进入api/values很烦,把webapi项目的启动路径设置成 swagger。这样每次调试运行项目都会自动跳转到swagger帮助页面
其余的请看这个大神的 https://www.cnblogs.com/suxinlcq/p/6757556.html
那是一座岛,岛上有青草,鲜花,美丽的走兽与飞鸟!