搭建带Swagger文档的API
1.创建解决方案
选中ASP.NET Core Web应用程序模板
选中API模板
2.运行出现简单的Api页面
3.在NuGet下载Swagger的依赖包“Swashbuckle.AspNetCore”
将ConfigureServices及Configure修改为如下
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
#region Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1.1.0",
Title = "WebApiDemo",
});
});
#endregion
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
#region Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");
});
#endregion
}
5.修改属性=>调试=>启动浏览器的数据为,swagger
5.运行