swagger的作用可以使我们的api文档更加的规范
废话不多说,直接讲在.netcore中怎么使用swagger,只需两个步骤即可完工
1 在nuget中直接引用swagger包
Swashbuckle.AspNetCore.Swagger
2 在Startup.cs文件中
1.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwagger();
//启用中间件服务对swagger-ui,指定Swagger JSON终结点
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "RebaTestApi V1");
});
}
2.
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
var xmlPath = Path.Combine(basePath, "swaggerApi.xml");
options.IncludeXmlComments(xmlPath);
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
{
Title = "swagger",
Version = "v1",
Description = "swagger API Swagger surface",
TermsOfService = "Terms Of Service"
});
});
}